home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / game / misc / WormWars.lha / WormWars / Source / system.c < prev    next >
C/C++ Source or Header  |  2001-04-21  |  110KB  |  3,074 lines

  1. /* $Filename: WormWars/Source/system.c $
  2.  * $VER:      Worm Wars 6.6 for Amiga $
  3.  *
  4.  * © Copyright 2001 James R. Jacobs. Freely distributable.
  5.  *        _
  6.  *       //   -=AMIGA=-
  7.  *      //
  8.  * _   //
  9.  * \\ //
  10.  *  \X/
  11.  
  12. #INCLUDES ---------------------------------------------------------------- */
  13.  
  14. #include "system.h"
  15. #include "amiga.h"
  16. #include "qdraw020.h"
  17.  
  18. #include <assert.h>
  19. // #define ASSERT
  20.  
  21. /* PROTOTYPES (Amiga-only) ----------------------------------------------- */
  22.  
  23. MODULE void freefx(void);
  24. MODULE void loadthefx(void);
  25. MODULE void loadthemusic(void);
  26. MODULE void parsewb(void);
  27. MODULE void pausetimer(void);
  28. MODULE void unpausetimer(void);
  29. MODULE void preserve(SWORD x, SBYTE row, SBYTE image, UWORD amount);
  30. MODULE ABOOL beginfx(void);
  31. MODULE UBYTE ReadJoystick(UWORD joynum);
  32.  
  33. AGLOBAL struct Window          *HelpWindowPtr = NULL,
  34.                                *MainWindowPtr = NULL;
  35. AGLOBAL struct Menu*            MenuPtr       = NULL;
  36. AGLOBAL struct VisualInfo*      VisualInfoPtr = NULL;
  37. AGLOBAL struct MsgPort*         JoyPortPtr    = NULL;
  38. AGLOBAL struct timerequest*     TimerRqPtr    = NULL;
  39. AGLOBAL struct InputEvent       GameEvent;
  40. AGLOBAL struct Screen*          ScreenPtr     = NULL;
  41. AGLOBAL ABOOL                   icons         = TRUE,
  42.                                 joy           = FALSE;
  43.  
  44. /* EXTERNAL VARIABLES (owned by engine.c, imported by system.c) ----------- */
  45.  
  46. IMPORT  struct ExecBase*        SysBase;
  47.  
  48. IMPORT  ABOOL                   modified, randomflag;
  49. IMPORT  SBYTE                   a, eachworm[4][2][9],
  50.                                 level, levels, players, sourcelevel,
  51.                                 startx[MAXLEVELS + 1], starty[MAXLEVELS + 1];
  52. IMPORT  SWORD                   secondsleft, secondsperlevel;
  53. IMPORT  ULONG                   delay, r;
  54. IMPORT  UBYTE                   board[MAXLEVELS + 1][FIELDX + 1][FIELDY + 1],
  55.                                 field[FIELDX + 1][FIELDY + 1];
  56. IMPORT    struct HiScoreStruct    hiscore[HISCORES + 1];
  57. IMPORT    struct WormStruct       worm[4];
  58. IMPORT  TEXT                    pathname[81],
  59.                                 date[DATELENGTH + 1],
  60.                                 times[TIMELENGTH + 1];
  61.  
  62.         struct Library*         TimerBase;
  63.         struct ASLBase*         ASLBase        = NULL;
  64. //      struct Library*         AmigaGuideBase = NULL;
  65.         struct DiskFontBase*    DiskFontBase   = NULL;
  66.         struct GadToolsBase*    GadToolsBase   = NULL;
  67.         struct GfxBase*         GfxBase        = NULL;
  68.         struct Library*         IconBase       = NULL;
  69.         struct IntuitionBase*   IntuitionBase  = NULL;
  70.         struct MEDPlayerBase*   MEDPlayerBase  = NULL;
  71.         struct UtilityBase*     UtilityBase    = NULL;
  72.  
  73. /* MODULE VARIABLES (used only within system.c) --------------------------- */
  74.  
  75. MODULE  ABOOL  eversent[4],
  76.                ignore       = FALSE,
  77.                iso          = TRUE,
  78.                quiet        = FALSE;
  79. MODULE  SBYTE  AudioClosed  = TRUE,
  80.                Controller   = GPCT_NOCONTROLLER,
  81.                JoyClosed    = TRUE,
  82.                OldPri       = 0,
  83.                TimerClosed  = TRUE;
  84. MODULE  UBYTE  fxable       = UNTRIED,
  85.                mode         = NULL,
  86.                musicable    = UNTRIED;
  87. MODULE  ULONG  fsize,
  88.                receipter[4] = {(ULONG) -1, (ULONG) -1, (ULONG) -1, (ULONG) -1};
  89. MODULE  UBYTE* fbase        = NULL;
  90. MODULE  BPTR   FilePtr      = NULL;
  91. MODULE  APTR   OldWindowPtr = NULL;
  92. MODULE  SWORD  pixy;
  93.  
  94. MODULE  struct QDPlaneInfo    QDPI;
  95. MODULE  ABOOL                 useqdraw            = FALSE;
  96.  
  97. MODULE    struct TextAttr       WormWars8 =
  98. {    (STRPTR) "WormWars.font", 8, FS_NORMAL, FPF_DISKFONT | FPF_DESIGNED
  99. },                            Topaz8 =
  100. {    (STRPTR) "topaz.font", 8, FS_NORMAL, FPF_ROMFONT | FPF_DESIGNED
  101. };
  102.  
  103. MODULE    struct Gadget         *CheckboxGadgetPtr  = NULL,
  104.                               *CycleGadgetPtr[4]  = {NULL, NULL, NULL, NULL},
  105.                               *GListPtr           = NULL,
  106.                               *PrevGadgetPtr      = NULL,
  107.                               *RandomGadgetPtr    = NULL,
  108.                               *StringGadgetPtr[5] = {NULL, NULL, NULL, NULL, NULL};
  109. MODULE    struct MsgPort        *AudioPortPtr[4]    = {NULL, NULL, NULL, NULL},
  110.                               *TimerPortPtr       = NULL;
  111. MODULE    struct timeval        *CurrentValPtr      = NULL,
  112.                               *PausedValPtr       = NULL,
  113.                               *StartValPtr        = NULL;
  114.  
  115. MODULE  struct CIA*           CIAPtr              = (struct CIA *) 0xBFE001;
  116. MODULE    struct RDArgs*        ArgsPtr             = NULL;
  117. MODULE    struct FileRequester* ASLRqPtr            = NULL;
  118. MODULE    struct IOAudio*       AudioRqPtr[4]       = {NULL, NULL, NULL, NULL};
  119. MODULE    struct TextFont*      FontPtr             = NULL;
  120. MODULE    struct IOStdReq*      JoyRqPtr            = NULL;
  121. MODULE    struct Process*       ProcessPtr          = NULL;
  122. MODULE    struct MMD0*          SongPtr             = NULL;
  123. MODULE    struct WBArg*         WBArg               = NULL;
  124. MODULE    struct WBStartup*     WBMsg               = NULL;
  125.  
  126. /* FUNCTIONS -------------------------------------------------------------- */
  127.  
  128. int main(int argc, char** argv)
  129. {
  130. BPTR    OldDir;
  131. SLONG   i;
  132. ABOOL   success = FALSE;
  133. SBYTE   error = 0,
  134.         player, which;
  135. TEXT    saystring[SAYLIMIT + 1];
  136. SWORD   oldsecondsleft;
  137. UWORD   Pens[10] =
  138. {       BLACK,          /* DETAILPEN            text in title bar */
  139.         WHITE,          /* BLOCKPEN             fill title bar */
  140.         WHITE,          /* TEXTPEN              regular text on BACKGROUNDPEN */
  141.         LIGHTGREY,      /* SHINEPEN             bright edge */
  142.         DARKGREY,       /* SHADOWPEN            dark edge */
  143.         PURPLE,         /* FILLPEN              filling active window borders
  144.                                                 and selected gadgets */
  145.         BLACK,          /* FILLTEXTPEN          text rendered over FILLPEN */
  146.         BLACK,          /* BACKGROUNDPEN        background colour */
  147.         RED,            /* HIGHLIGHTTEXTPEN     highlighted text on BACKGROUNDPEN */
  148.         (UWORD) ~0      /* and used against BLOCKPEN in ASL save requesters */
  149. };
  150. SLONG                   args[12] = {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
  151. struct ColorSpec        Colours[21] =
  152. {   /* colour   red   green   blue   description */
  153.     {   0,      0x0,    0x0,    0x0},   /*        BLACK */
  154.     {   1,      0xF,    0xF,    0xF},   /*        WHITE */
  155.     {   2,      0x5,    0x5,    0x5},   /*    DARKGREY */
  156.     {   3,      0x9,    0x9,    0x9},   /*  MEDIUMGREY */
  157.     {   4,      0xB,    0xB,    0xB},   /*   LIGHTGREY */
  158.     {   5,      0x0,    0x0,    0x0},   /*        black */
  159.     {   6,      0xC,    0x5,    0xC},   /*        PURPLE */
  160.     {   7,      0x8,    0x4,    0x2},   /*        brown */
  161.     {   8,      0x3,    0x9,    0x3},   /*    DARKGREEN */
  162.     {   9,      0x4,    0xE,    0x4},   /*  light GREEN */
  163.     {   10,     0xF,    0x1,    0x1},   /*    DARKRED */
  164.     {   11,     0xF,    0x5,    0x5},   /*  light RED */
  165.     {   12,     0x3,    0x3,    0xF},   /*    DARKBLUE */
  166.     {   13,     0x6,    0x6,    0xF},   /*  light BLUE */
  167.     {   14,     0xA,    0xA,    0x2},   /*    DARKYELLOW */
  168.     {   15,     0xC,    0xC,    0x2},   /*  light YELLOW */
  169.     {   16,     0x0,    0x0,    0x0},   /* pointer: transparent */
  170.     {   17,     0xE,    0x4,    0x4},   /* pointer: fill */
  171.     {   18,     0x3,    0x3,    0x3},   /* pointer: shadow */
  172.     {   19,     0xC,    0xC,    0xC},   /* pointer: shine */
  173.     {   -1,     NULL,   NULL,   NULL}
  174. };
  175.  
  176. /* Start of program.
  177.  
  178. version embedding into executable */
  179.  
  180. if (0) /* that is, never */
  181.     say(VERSION, ANYTHING);
  182.  
  183. if (!(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37L)))
  184. {    Write(Output(), OLDKICKSTART, strlen(OLDKICKSTART));
  185.     cleanexit(EXIT_FAILURE);
  186. }
  187.  
  188. /* From this point onwards, we can be sure we have Kickstart 2.04+... */
  189.  
  190. for (i = 0; i <= SAMPLES; i++)
  191.     samp[i].base = NULL;
  192. enginesetup();
  193. ProcessPtr = (struct Process *) FindTask(NULL);
  194.  
  195. if (SysBase->LibNode.lib_Version < 36L)
  196. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Need exec.library V36+!\0", 24);
  197.     cleanexit(EXIT_FAILURE);
  198. }
  199. if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L)))
  200. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open graphics.library!\0", 24);
  201.     cleanexit(EXIT_FAILURE);
  202. }
  203. if (!(GadToolsBase = (struct GadToolsBase *) OpenLibrary("gadtools.library", 37L)))
  204. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open GadTools.library V37+!\0", 24);
  205.     cleanexit(EXIT_FAILURE);
  206. }
  207. if (!(ASLBase = (struct ASLBase *) OpenLibrary("asl.library", 0L)))
  208. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open ASL.library!\0", 24);
  209.     cleanexit(EXIT_FAILURE);
  210. }
  211. if (!(UtilityBase = (struct UtilityBase *) OpenLibrary("utility.library", 0L)))
  212. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open utility.library!\0", 24);
  213.     cleanexit(EXIT_FAILURE);
  214. }
  215. if (!(IconBase = (struct Library *) OpenLibrary("icon.library", 0L)))
  216. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open icon.library!\0", 24);
  217.         cleanexit(EXIT_FAILURE);
  218. }
  219.  
  220. /* argument parsing */
  221.  
  222. if (argc) /* started from CLI */
  223. {   if (!(ArgsPtr = ReadArgs
  224.     (   "-F=NOFX/S,-M=NOMUSIC/S,-I=NOICONS/S,-P=PRI/K/N,-O=OVERHEAD/S,"
  225.         "-S=SHUFFLE/S,-Q=QUIET/S,GREEN/K,RED/K,BLUE/K,YELLOW/K,FILE",
  226.         (LONG *) args,
  227.         NULL
  228.     )))
  229.     {   Printf
  230.         (   "Usage: %s [-f=NOFX] [-m=NOMUSIC] [-i=NOICONS] "
  231.             "[-p=PRI <priority>] [-o=OVERHEAD] [-s=SHUFFLE] [-q=QUIET] "
  232.             "[GREEN=KYBD|JOY|AMIGA|NONE] "
  233.             "[RED=KYBD|JOY|AMIGA|NONE] [BLUE=HUMAN|AMIGA|NONE] "
  234.             "[YELLOW=HUMAN|AMIGA|NONE] [[FILE=]<fieldset>]\n",
  235.             argv[0]
  236.         );
  237.         cleanexit(EXIT_FAILURE);
  238.     }
  239.     if (args[0])
  240.         fxable = DEFER;
  241.     if (args[1])
  242.         musicable = DEFER;
  243.     if (args[2])
  244.         icons = FALSE;
  245.     if (args[3])
  246.     {   if (args[3] < -128 || args[3] > 5)
  247.         {   Printf("%s: Priority range is -128 to +5\n", argv[0]);
  248.             cleanexit(EXIT_FAILURE);
  249.         }
  250.         OldPri = SetTaskPri((struct Task *) ProcessPtr, args[3]);
  251.     }
  252.     if (args[4])
  253.         iso = FALSE;
  254.     if (args[5])
  255.         randomflag = TRUE;
  256.     if (args[6])
  257.     {   quiet = TRUE;
  258.     }
  259.     if (args[7])
  260.     {   if (!stricmp(args[7], "KYBD"))
  261.         {   worm[0].control = KEYBOARD;
  262.             worm[0].control = KEYBOARD;
  263.         } elif (!stricmp(args[7], "JOY"))
  264.         {   worm[0].control = JOYSTICK;
  265.             worm[0].control = JOYSTICK;
  266.         } elif (!stricmp(args[7], "AMIGA"))
  267.         {   worm[0].control = AMIGA;
  268.         } elif (!stricmp(args[7], "NONE"))
  269.         {   worm[0].control = NONE;
  270.         } else
  271.         {   Printf("%s: Green worm control must be KYBD, JOY, AMIGA or NONE\n", argv[0]);
  272.             cleanexit(EXIT_FAILURE);
  273.     }   }
  274.     if (args[8])
  275.     {   if (!stricmp(args[8], "KYBD"))
  276.         {   worm[1].control = KEYBOARD;
  277.             worm[1].control = KEYBOARD;
  278.         } elif (!stricmp(args[8], "JOY"))
  279.         {   worm[1].control = JOYSTICK;
  280.             worm[1].control = JOYSTICK;
  281.         } elif (!stricmp(args[8], "AMIGA"))
  282.         {   worm[1].control = AMIGA;
  283.         } elif (!stricmp(args[8], "NONE"))
  284.         {   worm[1].control = NONE;
  285.         } else
  286.         {   Printf("%s: Red worm control must be KYBD, JOY, AMIGA or NONE\n", argv[0]);
  287.             cleanexit(EXIT_FAILURE);
  288.     }   }
  289.     if (args[9])
  290.     {    if (!stricmp(args[9], "JOY"))
  291.             worm[2].control = JOYSTICK;
  292.         elif (!stricmp(args[9], "AMIGA"))
  293.             worm[2].control = AMIGA;
  294.         elif (!stricmp(args[9], "NONE"))
  295.             worm[2].control = NONE;
  296.         else
  297.         {    Printf("%s: Blue worm control must be JOY, AMIGA or NONE\n", argv[0]);
  298.             cleanexit(EXIT_FAILURE);
  299.     }    }
  300.     if (args[10])
  301.     {    if (!stricmp(args[10], "JOY"))
  302.             worm[3].control = JOYSTICK;
  303.         elif (!stricmp(args[10], "AMIGA"))
  304.             worm[3].control = AMIGA;
  305.         elif (!stricmp(args[10], "NONE"))
  306.             worm[3].control = NONE;
  307.         else
  308.         {    Printf("%s: Yellow worm control must be JOY, AMIGA or NONE\n", argv[0]);
  309.             cleanexit(EXIT_FAILURE);
  310.     }    }
  311.     if (args[11])
  312.         {   strcpy(pathname, args[11]);
  313.     }   }
  314.     else /* started from WB */
  315.     {   WBMsg = (struct WBStartup *) argv;
  316.     WBArg = WBMsg->sm_ArgList; /* head of the arg list */
  317.  
  318.     for (i = 0;
  319.          i < WBMsg->sm_NumArgs;
  320.          i++, WBArg++)
  321.     {    if (WBArg->wa_Lock)
  322.         {    /* something that does not support locks */
  323.             parsewb();
  324.         }
  325.         else
  326.         {    /* locks supported, change to the proper directory */
  327.             OldDir = CurrentDir(WBArg->wa_Lock);
  328.             parsewb();
  329.             CurrentDir(OldDir);
  330.                 }
  331.                 if (i == 1)
  332.                 {    strcpy(pathname, WBArg->wa_Name);
  333. }       }       }
  334.  
  335. if (!(DiskFontBase = (struct DiskFontBase *) OpenLibrary("diskfont.library", 0L)))
  336.     error = 1;
  337.     
  338. if
  339. (       (!(StartValPtr = (struct timeval *) AllocMem(sizeof(struct timeval), MEMF_PUBLIC | MEMF_CLEAR)))
  340. ||      (!(CurrentValPtr = (struct timeval *) AllocMem(sizeof(struct timeval), MEMF_PUBLIC | MEMF_CLEAR)))
  341. ||      (!(PausedValPtr = (struct timeval *) AllocMem(sizeof(struct timeval), MEMF_PUBLIC | MEMF_CLEAR)))
  342. )
  343. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't allocate timer value structure!\0", 24);
  344.     cleanexit(EXIT_FAILURE);
  345. }
  346. if (!(TimerPortPtr = (struct MsgPort *) CreateMsgPort()))
  347. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't allocate timer message port!\0", 24);
  348.     cleanexit(EXIT_FAILURE);
  349. }
  350. if (!(TimerRqPtr = (struct timerequest *) CreateIORequest(TimerPortPtr, sizeof(struct timerequest))))
  351. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't create timer I/O request!\0", 24);
  352.     cleanexit(EXIT_FAILURE);
  353. }
  354. if (TimerClosed = OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *) TimerRqPtr, 0))
  355. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open timer.device!\0", 24);
  356.     cleanexit(EXIT_FAILURE);
  357. }
  358. TimerBase = (struct Library *) TimerRqPtr->tr_node.io_Device;
  359.  
  360. /* PREPARE DISPLAY -----------------------------------------------
  361.  
  362. font and screen */
  363.  
  364. if ((!error) && (FontPtr = (struct TextFont *) OpenDiskFont(&WormWars8)))
  365. {   /* loaded WormWars.font */
  366.  
  367.     ScreenPtr = (struct Screen *) OpenScreenTags
  368.     (   NULL,
  369.         SA_Width,       640,
  370.         SA_Height,      256,
  371.         SA_Depth,       DEPTH,
  372.         SA_DisplayID,   HIRES_KEY | PAL_MONITOR_ID,
  373.         SA_Title,       TITLEBAR,
  374.         SA_Colors,      Colours,
  375.         SA_Font,        &WormWars8,
  376.         SA_Pens,        Pens,
  377.         SA_PubName,     "WormWars",
  378.         TAG_DONE
  379.     );
  380.     if (!ScreenPtr)
  381.     {   DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open screen!\0", 24);
  382.         cleanexit(EXIT_FAILURE);
  383. }   }
  384. else
  385. {   if (!error)
  386.         error = 2;
  387.  
  388.     ScreenPtr = (struct Screen *) OpenScreenTags
  389.     (   NULL,
  390.         SA_Width,       640,
  391.         SA_Height,      256,
  392.         SA_Depth,       DEPTH,
  393.         SA_DisplayID,   HIRES_KEY | PAL_MONITOR_ID,
  394.         SA_Title,       TITLEBAR,
  395.         SA_Colors,      Colours,
  396.         SA_Font,        &Topaz8,
  397.         SA_Pens,        Pens,
  398.         SA_PubName,     "WormWars",
  399.         TAG_DONE
  400.     );
  401.     if (!ScreenPtr)
  402.     {   DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open screen nor font!\0", 24);
  403.         cleanexit(EXIT_FAILURE);
  404. }   }
  405.  
  406. PubScreenStatus(ScreenPtr, NULL); // take the screen public
  407.  
  408. /* For some reason, we lose 4 memory chunks around this point: 2 chunks
  409. each of 292 bytes and 10 bytes. It is related to OpenScreenTagList(),
  410. but is not related to the SA_Colors, SA_Font or SA_Pens tags, nor to
  411. the timer.device.
  412.  
  413. Now that the screen is set up, we initialize the turbo graphics as
  414. follows: */
  415.  
  416. for (i = 0; i < DEPTH; i++)
  417.     QDPI.pi_Planes[i] = ScreenPtr->RastPort.BitMap->Planes[i];
  418. QDPI.pi_BPR           = ScreenPtr->RastPort.BitMap->BytesPerRow;
  419. QDPI.pi_PlaneSZ       = ScreenPtr->RastPort.BitMap->BytesPerRow *
  420.                         ScreenPtr->RastPort.BitMap->Rows;
  421.  
  422. /* GetBitMapAttr() is more forwards-compatible than direct structure
  423. access. */
  424. useqdraw = (SysBase->AttnFlags & AFB_68020);
  425.  
  426. /* GadTools */
  427.  
  428. if (!(CycleGadget.ng_VisualInfo = StringGadget.ng_VisualInfo = CheckboxGadget.ng_VisualInfo = RandomGadget.ng_VisualInfo = VisualInfoPtr = (APTR) GetVisualInfo(ScreenPtr, TAG_DONE)))
  429. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't get GadTools visual info!\0", 24);
  430.     cleanexit(EXIT_FAILURE);
  431. }
  432. if (!(MenuPtr = (struct Menu *) CreateMenus(NewMenu, TAG_DONE)))
  433. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't create menus!\0", 24);
  434.     cleanexit(EXIT_FAILURE);
  435. }
  436. if (!(LayoutMenus(MenuPtr, VisualInfoPtr, TAG_DONE)))
  437. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't lay out menus!\0", 24);
  438.     cleanexit(EXIT_FAILURE);
  439. }
  440. if (!(PrevGadgetPtr = (struct Gadget *) CreateContext(&GListPtr)))
  441. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't create GadTools context!\0", 24);
  442.     cleanexit(EXIT_FAILURE);
  443. }
  444.  
  445.     for (player = 0; player <= 3; player++)
  446.     {   CycleGadget.ng_TopEdge = TSOFFSET + 84 + (player * (FONTX + 8));
  447.     CycleGadget.ng_GadgetText = CycleText[player];
  448.     CycleGadgetPtr[player] = PrevGadgetPtr = (struct Gadget *) CreateGadget
  449.         (   CYCLE_KIND,
  450.             PrevGadgetPtr,
  451.             &CycleGadget,
  452.             GTCY_Labels, CycleOptions[player],
  453.             GTCY_Active, worm[player].control,
  454.             GT_Underscore, '_',
  455.             GA_Disabled, TRUE,
  456.             TAG_DONE
  457.     );
  458.     }
  459.     RandomGadgetPtr = PrevGadgetPtr = (struct Gadget *) CreateGadget
  460.     (   CHECKBOX_KIND,
  461.     PrevGadgetPtr,
  462.     &RandomGadget,
  463.     GTCB_Checked, randomflag,
  464.     GT_Underscore, '_',
  465.     GA_Disabled, TRUE,
  466.     TAG_DONE
  467.     );
  468.     CheckboxGadgetPtr = PrevGadgetPtr = (struct Gadget *) CreateGadget
  469.     (   CHECKBOX_KIND,
  470.     PrevGadgetPtr,
  471.     &CheckboxGadget,
  472.     GTCB_Checked, iso,
  473.     GT_Underscore, '_',
  474.     GA_Disabled, TRUE,
  475.     TAG_DONE
  476.     );
  477.  
  478. /* main window */
  479.  
  480. if (!(MainWindowPtr = (struct Window *) OpenWindowTags(NULL,
  481.         WA_Top,                 11,
  482.         WA_Width,               SCREENXPIXEL,
  483.         WA_Height,              SCREENYPIXEL,
  484.         WA_IDCMP,               IDCMP_RAWKEY | IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_MENUPICK | IDCMP_MENUVERIFY | CYCLEIDCMP | STRINGIDCMP | CHECKBOXIDCMP | IDCMP_REFRESHWINDOW | IDCMP_INTUITICKS,
  485.         WA_Gadgets,             GListPtr,
  486.     WA_CustomScreen,    ScreenPtr,
  487.     WA_Borderless,        TRUE,
  488.     WA_Activate,        TRUE,
  489.     WA_SmartRefresh,    TRUE,
  490.     WA_RptQueue,        16,
  491.     TAG_DONE)))
  492.     {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't open main window!\0", 24);
  493.         cleanexit(EXIT_FAILURE);
  494.     }
  495.  
  496. /* redirection of AmigaDOS system requesters */
  497.     
  498. OldWindowPtr = ProcessPtr->pr_WindowPtr;
  499. ProcessPtr->pr_WindowPtr = (APTR) MainWindowPtr;
  500.  
  501. if (!(ASLRqPtr = AllocAslRequestTags(ASL_FileRequest, ASL_Pattern, PATTERN, ASL_Window, MainWindowPtr, TAG_DONE)))
  502. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't create ASL request!\0", 24);
  503.     cleanexit(EXIT_FAILURE);
  504. }
  505.  
  506. /* String gadgets: first the window is opened with the cycle and
  507. checkbox gadgets.
  508.     It is necessary not to display the string gadgets yet, so the
  509. gadgets are then created and added to the gadget list. You will note
  510. there is no command given to render them as yet. When the attributes
  511. are modified at highscore time, the applicable gadget is refreshed
  512. then. */
  513.  
  514. for (which = 0; which <= HISCORES; which++)
  515. {       StringGadget.ng_TopEdge = TSOFFSET + (which * HISCOREDISTANCE);
  516.     StringGadgetPtr[which] = PrevGadgetPtr = (struct Gadget *) CreateGadget(STRING_KIND, PrevGadgetPtr, &StringGadget, GTST_MaxChars, NAMELENGTH, STRINGA_ReplaceMode, TRUE, GA_Disabled, TRUE, TAG_DONE);
  517. }
  518. if (!PrevGadgetPtr)
  519. {    DisplayAlert(AT_Recovery, "\0\20\20Worm Wars: Can't create GadTools gadgets!\0", 24);
  520.     cleanexit(EXIT_FAILURE);
  521. }
  522.  
  523.     if (!icons)
  524.         NewMenu[CREATEICONS].nm_Flags &= ~CHECKED;
  525.  
  526.     if (!quiet)
  527.     {   if (error == 1)
  528.         {   say("Can't open diskfont.library!", RED);
  529.             anykey(TRUE);
  530.         } elif (error == 2)
  531.         {   say("Can't open WormWars.font!", RED);
  532.             anykey(TRUE);
  533.     }   }
  534.  
  535.     if (!(JoyPortPtr = (struct MsgPort *) CreateMsgPort()))
  536.     {   if (!quiet)
  537.         {   say("Can't create joystick message port!", BLUE);
  538.             anykey(TRUE);
  539.     }   }
  540.     elif (!(JoyRqPtr = (struct IOStdReq *) CreateIORequest(JoyPortPtr, sizeof(struct IOStdReq))))
  541.     {   if (!quiet)
  542.         {   say("Can't create joystick I/O request!", BLUE);
  543.         anykey(TRUE);
  544.     }   }
  545.     elif (JoyClosed = OpenDevice("gameport.device", 1, (struct IORequest *) JoyRqPtr, 0))
  546.     {   if (!quiet)
  547.         {   say("Can't open gameport.device!", BLUE);
  548.             anykey(TRUE);
  549.         } else
  550.         {   Forbid();
  551.             JoyRqPtr->io_Command = GPD_ASKCTYPE;
  552.         JoyRqPtr->io_Length  = 1;
  553.             JoyRqPtr->io_Flags   = IOF_QUICK;
  554.             JoyRqPtr->io_Data    = (APTR) &Controller;
  555.             DoIO((struct IORequest *) JoyRqPtr);
  556.             if (Controller == GPCT_NOCONTROLLER)
  557.             {   Controller           = GPCT_ABSJOYSTICK;
  558.                 JoyRqPtr->io_Command = GPD_SETCTYPE;
  559.                 JoyRqPtr->io_Length  = 1;
  560.                 JoyRqPtr->io_Data    = (APTR) &Controller;
  561.                 DoIO((struct IORequest *) JoyRqPtr);
  562.                 success = TRUE;
  563.             }
  564.             Permit();
  565.             /* Note that say(), anykey() calls must be outside the
  566.             critical section (Forbid()/Permit() pair). */
  567.             if (success)
  568.             {    JoyRqPtr->io_Command    = GPD_SETTRIGGER;
  569.                 JoyRqPtr->io_Data        = (APTR) &Trigger;
  570.                 JoyRqPtr->io_Length        = sizeof(struct GamePortTrigger);
  571.                 DoIO((struct IORequest *) JoyRqPtr);
  572.                 sendreadrequest();
  573.                 joy = TRUE;
  574.             } else
  575.             {   if (!quiet)
  576.                 {   say("Gameport already in use!", BLUE);
  577.                     anykey(TRUE);
  578.     }   }   }   }
  579.  
  580.     if (musicable == UNTRIED)
  581.         loadthemusic();
  582.     if (fxable == UNTRIED)
  583.         loadthefx();
  584.  
  585.     if (fxable == SUCCEEDED)
  586.         toggle(F);
  587.     else if (musicable == SUCCEEDED)
  588.         toggle(M);
  589.  
  590.     strcpy(saystring, "Loading ");
  591.     strcat(saystring, pathname);
  592.     strcat(saystring, "...");
  593.     say(saystring, WHITE);
  594.     if (loadfields(pathname))
  595.     {   if (!quiet)
  596.         {   strcpy(saystring, "Can't open ");
  597.             strcat(saystring, pathname);
  598.             strcat(saystring, "!");
  599.             say(saystring, RED);
  600.         }
  601.         newfields();
  602.         if (!quiet)
  603.         {   anykey(TRUE);
  604.     }   }
  605.  
  606.     while (1)
  607.     {   titlescreen();
  608.     
  609.         /* MAIN GAME LOOP ------------------------------------------------- */
  610.     
  611.         while (a == PLAYGAME)
  612.         {   r++;
  613.             oldsecondsleft = secondsleft;
  614.             GetSysTime(CurrentValPtr);
  615.             SubTime(CurrentValPtr, StartValPtr);
  616.             secondsleft = secondsperlevel - CurrentValPtr->tv_secs;
  617.             if (secondsleft != oldsecondsleft)
  618.                 timeloop();
  619.             TimerRqPtr->tr_node.io_Command  = TR_ADDREQUEST;
  620.             TimerRqPtr->tr_time.tv_secs     = 0;
  621.             TimerRqPtr->tr_time.tv_micro    = delay;
  622.             SendIO((struct IORequest *) TimerRqPtr);
  623.             gameloop();
  624.             // CheckIO() returns FALSE if complete, pointer if incomplete.
  625.             if (CheckIO((struct IORequest *) TimerRqPtr)) // CheckIO returns pointer to struct IORequest
  626.                 draw(CLOCKICON, ICONY, CLOCK);
  627.             else draw(CLOCKICON, ICONY, BLACKENED);
  628.             WaitIO((struct IORequest *) TimerRqPtr);
  629.         }
  630.         say("Title Screen", WHITE);
  631. }   }
  632.     
  633. /* SUPPORT FUNCTIONS ----------------------------------------------------- */
  634.     
  635. /* NAME     anykey -- wait for a user press
  636. SYNOPSIS    anykey(ABOOL);
  637. FUNCTION    Waits for a user response. Optional automatic timeout.
  638. INPUTS      timeout - timeout?
  639. RESULTS     FALSE if user presses Escape, TRUE otherwise.
  640. FILE        system.c */
  641.  
  642. ABOOL anykey(ABOOL timeout)
  643. {    ABOOL                    done        = FALSE;
  644.     SBYTE                    count        = 0;
  645.     UWORD                    code, qual;
  646.     ULONG                    class;
  647.     struct IntuiMessage*    MsgPtr;
  648.  
  649.     clearkybd();
  650.     Forbid();
  651.     MainWindowPtr->Flags |= WFLG_RMBTRAP;
  652.     Permit();
  653.     
  654.     while (!done)
  655.     {    if (joy && GetMsg(JoyPortPtr))
  656.         {    if (GameEvent.ie_Code == IECODE_LBUTTON)
  657.                 done = TRUE;
  658.             sendreadrequest();
  659.         }
  660.         if (joy)
  661.         {    Wait((1L << MainWindowPtr->UserPort->mp_SigBit) | (1L << JoyPortPtr->mp_SigBit));
  662.             while (GetMsg(JoyPortPtr))
  663.             {    if (GameEvent.ie_Code == IECODE_LBUTTON)
  664.                     done = TRUE;
  665.                 sendreadrequest();
  666.         }    }
  667.         else Wait(1L << MainWindowPtr->UserPort->mp_SigBit);
  668.         while (MsgPtr = (struct IntuiMessage *) GT_GetIMsg(MainWindowPtr->UserPort))
  669.         {    class = MsgPtr->Class;
  670.             code  = MsgPtr->Code;
  671.             qual  = MsgPtr->Qualifier;
  672.             GT_ReplyIMsg(MsgPtr);
  673.             switch(class)
  674.             {
  675.             case IDCMP_RAWKEY:
  676.                             if ((!(qual & IEQUALIFIER_REPEAT)) && code < KEYUP && (code < FIRSTQUALIFIER || code > LASTQUALIFIER))
  677.                             {   done = TRUE;
  678.                                 if (code == M)
  679.                                     toggle(M);
  680.                                 else if (code == F)
  681.                                     toggle(F);
  682.                                 else if (code == ESCAPE)
  683.                                 {   if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  684.                                         if (verify())
  685.                                             cleanexit(EXIT_SUCCESS);
  686.                                     else
  687.                                     {   Forbid();
  688.                                         MainWindowPtr->Flags &= ~WFLG_RMBTRAP;
  689.                                         Permit();
  690.                                         return FALSE;
  691.                             }   }   }
  692.                         break;
  693.             case IDCMP_CLOSEWINDOW:
  694.                 cleanexit(EXIT_SUCCESS);
  695.                 break;
  696.             case IDCMP_ACTIVEWINDOW:
  697.                 ignore = TRUE;
  698.                 break;
  699.             case IDCMP_MOUSEBUTTONS:
  700.                 if ((code == SELECTDOWN || code == MENUDOWN) && !(qual & IEQUALIFIER_REPEAT))
  701.                     if (ignore)
  702.                         ignore = FALSE;
  703.                     else done = TRUE;
  704.                 break;
  705.             case IDCMP_INTUITICKS:
  706.                 if (timeout && ++count > PATIENCE)
  707.                     done = TRUE;
  708.                 break;
  709.             default:
  710.                 break;
  711.     }    }    }
  712.  
  713.     Forbid();
  714.     MainWindowPtr->Flags &= ~WFLG_RMBTRAP;
  715.     Permit();
  716.     return TRUE;
  717. }
  718.  
  719. void celebrate(void)
  720. {    ABOOL                    done = FALSE;
  721.     ULONG                    class;
  722.     UWORD                    code, qual;
  723.     struct IntuiMessage*    MsgPtr;
  724.     UBYTE                   player;
  725.  
  726.     for (player = 0; player <= 3; player++)
  727.         if (worm[player].control != NONE && worm[player].score >= worm[player].hiscore)
  728.              worm[player].hiscore = worm[player].score;
  729.     waitasec();
  730.     clearkybd();
  731.     while (!done)
  732.     {    while (MsgPtr = (struct IntuiMessage *) GT_GetIMsg(MainWindowPtr->UserPort))
  733.         {    class = MsgPtr->Class;
  734.             code  = MsgPtr->Code;
  735.             qual  = MsgPtr->Qualifier;
  736.             GT_ReplyIMsg(MsgPtr);
  737.             switch (class)
  738.             {
  739.             case IDCMP_RAWKEY:
  740.                 if (code == ESCAPE)
  741.                 {    if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  742.                     {    if (verify())
  743.                             cleanexit(EXIT_SUCCESS);
  744.                     } else
  745.                         done = TRUE;
  746.                 }
  747.                 else if (code == RETURN || code == ENTER || code == SPACEBAR)
  748.                     done = TRUE;
  749.                 else if (code == M)
  750.                     toggle(M);
  751.                 else if (code == F)
  752.                     toggle(F);
  753.                 break;
  754.             case IDCMP_MOUSEBUTTONS:
  755.                 if (code == SELECTDOWN && !(qual & IEQUALIFIER_REPEAT))
  756.                     if (ignore)
  757.                         ignore = FALSE;
  758.                     else done = TRUE;
  759.                 break;
  760.             case IDCMP_ACTIVEWINDOW:
  761.                 ignore = TRUE;
  762.                 break;
  763.             case IDCMP_CLOSEWINDOW:
  764.                 cleanexit(EXIT_SUCCESS);
  765.                 break;
  766.             default:
  767.                 break;
  768.         }   }
  769.         if (joy && GetMsg(JoyPortPtr))
  770.         {   if (GameEvent.ie_Code == IECODE_LBUTTON)
  771.                 done = TRUE;
  772.             sendreadrequest();
  773.         }
  774.         SetRGB4(&ScreenPtr->ViewPort, 17, rand() % 16, rand() % 16, rand() % 16);
  775.         draw(rand() % (FIELDX + 1), rand() % (FIELDY + 1), rand() % LASTOBJECT);
  776.     }
  777.     SetRGB4(&ScreenPtr->ViewPort, 17, 14, 4, 4);
  778.     a = GAMEOVER;
  779. }
  780.  
  781. void cleanexit(SLONG rc)
  782. {   SBYTE i;
  783.     struct Message* MsgPtr;
  784.  
  785. // CheckIO() returns NULL if complete, pointer if incomplete?
  786. if (TimerRqPtr && (!CheckIO((struct IORequest *) TimerRqPtr)))
  787.                         {       AbortIO((struct IORequest *) TimerRqPtr);
  788.                                 WaitIO((struct IORequest *) TimerRqPtr);
  789.                         }
  790.                                 freefx();
  791.                                 for (i = 0; i <= SAMPLES; i++)
  792.                                     if (samp[i].base)
  793.                                         FreeMem(samp[i].base, samp[i].size);
  794. if (mode == MUSIC)        StopPlayer();
  795. if (SongPtr)            UnLoadModule(SongPtr);
  796. if (MEDPlayerBase)    {    FreePlayer();
  797.                                 CloseLibrary((struct Library *) MEDPlayerBase);
  798.                         }
  799. if (joy)                {       AbortIO((struct IORequest *) JoyRqPtr);
  800.                                 WaitIO((struct IORequest *) JoyRqPtr);
  801.                         }
  802. if (Controller != GPCT_NOCONTROLLER)
  803.                         {       Forbid();
  804.                                 Controller           = GPCT_NOCONTROLLER;
  805.                                 JoyRqPtr->io_Command = GPD_SETCTYPE;
  806.                                 JoyRqPtr->io_Length  = 1;
  807.                                 JoyRqPtr->io_Data    = (APTR) &Controller;
  808.                                 DoIO((struct IORequest *) JoyRqPtr);
  809.                                 Permit();
  810.                         }
  811. if (!JoyClosed)                 CloseDevice((struct IORequest *) JoyRqPtr);
  812. if (JoyRqPtr)           {       DeleteIORequest((struct IORequest *) JoyRqPtr);
  813.                                 while(MsgPtr = GetMsg(JoyPortPtr))
  814.                                     ReplyMsg(MsgPtr); // important, or we lose memory
  815.                         }
  816. if (JoyPortPtr)            DeleteMsgPort(JoyPortPtr);
  817.  
  818. if (ASLRqPtr)            FreeAslRequest(ASLRqPtr);
  819. if (OldWindowPtr)        ProcessPtr->pr_WindowPtr = OldWindowPtr;
  820. if (MainWindowPtr)    {    clearkybd();
  821.                                 ClearMenuStrip(MainWindowPtr);
  822.                                 CloseWindow(MainWindowPtr);                                             }
  823. if (GListPtr)            FreeGadgets(GListPtr);
  824. if (MenuPtr)            FreeMenus(MenuPtr);
  825. if (VisualInfoPtr)        FreeVisualInfo(VisualInfoPtr);
  826. if (ScreenPtr)            CloseScreen(ScreenPtr);
  827. if (FontPtr)            CloseFont(FontPtr);
  828. if (!TimerClosed)               CloseDevice((struct IORequest *) TimerRqPtr);
  829. if (TimerRqPtr)                 DeleteIORequest(TimerRqPtr);
  830. if (TimerPortPtr)        DeleteMsgPort(TimerPortPtr);
  831. if (PausedValPtr)        FreeMem(PausedValPtr, sizeof(struct timeval));
  832. if (CurrentValPtr)        FreeMem(CurrentValPtr, sizeof(struct timeval));
  833. if (StartValPtr)        FreeMem(StartValPtr, sizeof(struct timeval));
  834. if (DiskFontBase)        CloseLibrary((struct Library *) DiskFontBase);
  835. if (UtilityBase)        CloseLibrary((struct Library *) UtilityBase);
  836. if (ASLBase)            CloseLibrary((struct Library *) ASLBase);
  837. if (GadToolsBase)        CloseLibrary((struct Library *) GadToolsBase);
  838. if (GfxBase)            CloseLibrary((struct Library *) GfxBase);
  839. if (IconBase)            CloseLibrary((struct Library *) IconBase);
  840. if (IntuitionBase)    {    OpenWorkBench();
  841.                                 CloseLibrary((struct Library *) IntuitionBase);                                    }
  842.                                 SetTaskPri((struct Task *) ProcessPtr, OldPri);
  843. if (ArgsPtr)            FreeArgs(ArgsPtr);
  844.                                 exit(rc); /* End of program. */
  845. }
  846.  
  847. void clearjoystick(void)
  848. {   if (joy)
  849.         while (GetMsg(JoyPortPtr))
  850.             sendreadrequest();
  851. }
  852.  
  853. void clearkybd(void)
  854. {   struct IntuiMessage* MsgPtr;
  855.  
  856.     while (MsgPtr = (struct IntuiMessage *) GT_GetIMsg(MainWindowPtr->UserPort))
  857.         GT_ReplyIMsg(MsgPtr);
  858. }
  859.  
  860. void draw(SBYTE x, SBYTE y, UBYTE image)
  861. {   SWORD pixx;
  862.  
  863.     /* Isometric methodology:
  864.     
  865.     read the colour of the pixel, then set the colour in the image
  866.     to the same value.
  867.  
  868.     So if, for the top left pixel, we read colour 10 (= $A or %1010),
  869.     we have to clear the relevant bits in plane 0 and 2, and set them
  870.     in planes 1 and 3.
  871.  
  872.     plane 0 (least significant bit)
  873.         1234567890123456 pixels for top row (y = 0)
  874.         1234567890123456 pixels for next row (y = 1)
  875.         : : :
  876.     plane 1
  877.         : : :
  878.  
  879.      01234567890
  880.     0..#########
  881.     1..#########
  882.     2.#########.
  883.     3.#########.
  884.     4#########..
  885.     5#########..
  886.  
  887.     Image is effectively 9*6, but requires an 11*6 bitmap. */
  888.  
  889.     if (a == PLAYGAME && iso)
  890.     {   pixx = (x * ISOSQUAREX) + 20 + ((FIELDY - y) * 3);
  891.         pixy = (y * ISOSQUAREY) + STARTYPIXEL;
  892.  
  893.         if (useqdraw)
  894.         {   // Turbo field rendering routines, thanks to Jilles Tjoelker.
  895.             qdraw020iso
  896.             (   &QDPI,
  897.                 pixx,
  898.                 pixy + MainWindowPtr->TopEdge,
  899.                 (WORD *) IsoImageData[image]
  900.             );
  901.         }
  902.         else
  903.         {   IsoImage.ImageData = IsoImageData[image];
  904.             preserve(pixx,      0, image, 32768);
  905.             preserve(pixx,      1, image, 32768);
  906.             preserve(pixx,      2, image, 32768);
  907.             preserve(pixx,      3, image, 32768);
  908.             preserve(pixx +  1, 0, image, 16384);
  909.             preserve(pixx +  1, 1, image, 16384);
  910.             preserve(pixx +  9, 4, image,    64);
  911.             preserve(pixx +  9, 5, image,    64);
  912.             preserve(pixx + 10, 2, image,    32);
  913.             preserve(pixx + 10, 3, image,    32);
  914.             preserve(pixx + 10, 4, image,    32);
  915.             preserve(pixx + 10, 5, image,    32);
  916.             DrawImage
  917.             (   MainWindowPtr->RPort,
  918.                 &IsoImage,
  919.                 pixx,
  920.                 pixy
  921.             );
  922.     }   }
  923.     else
  924.     {   if (useqdraw)
  925.         {   // Turbo field rendering routines, thanks to Jilles Tjoelker.
  926.             qdraw020
  927.             (   &QDPI,
  928.                 STARTXPIXEL + (SQUAREX * x),
  929.                 STARTYPIXEL + (SQUAREY * y) + MainWindowPtr->TopEdge,
  930.                 (WORD *) ImageData[image]
  931.             );
  932.         }
  933.         else
  934.         {   Image.ImageData = ImageData[image];
  935.             DrawImage
  936.             (   MainWindowPtr->RPort,
  937.                 &Image,
  938.                 STARTXPIXEL + (SQUAREX * x),
  939.                 STARTYPIXEL + (SQUAREY * y)
  940.             );
  941. }   }   }
  942.  
  943. MODULE void preserve(SWORD x, SBYTE row, SBYTE image, UWORD amount)
  944. {    LONG colour;
  945.     
  946.     /* x = image top-left corner coordinate on destination screen.
  947.     row = y-row of the image.
  948.     image = image number
  949.     amount = value of the bit to be set/cleared. */
  950.  
  951.     colour = ReadPixel
  952.     (    MainWindowPtr->RPort,
  953.         x,
  954.         pixy + row
  955.     );
  956.     if (colour & 8)
  957.         IsoImageData[image][row + 18] |= amount;
  958.     else IsoImageData[image][row + 18] &= ~amount;
  959.     if (colour & 4)
  960.         IsoImageData[image][row + 12] |= amount;
  961.     else IsoImageData[image][row + 12] &= ~amount;
  962.     if (colour & 2)
  963.         IsoImageData[image][row + 6] |= amount;
  964.     else IsoImageData[image][row + 6] &= ~amount;
  965.     if (colour & 1)
  966.         IsoImageData[image][row] |= amount;
  967.     else IsoImageData[image][row] &= ~amount;
  968. }
  969.  
  970. void effect(SBYTE index)
  971. {   AUTO    SBYTE i;
  972.     AUTO    SBYTE ok            = -1;
  973.     AUTO    ULONG oldestreceipt = (ULONG) -1L;
  974.     PERSIST ULONG nextreceipt   = 1L;
  975.  
  976.     /* oldestreceipt = temporary variable for ascertaining oldest
  977.                        sound still playing.
  978.     nextreceipt = next unused receipt number (monotonically incrementing). */
  979.  
  980.     if (mode == FX)
  981.     {    for (i = 0; i <= 3; i++)
  982.         {    /* decide on a channel */
  983.  
  984.             if (ok == -1)
  985.             {    if (!eversent[i])
  986.                     ok = i;
  987.                                 // CheckIO() returns NULL if complete, pointer if incomplete.
  988.                                 elif (CheckIO((struct IORequest *) AudioRqPtr[i]))
  989.                 {    WaitIO((struct IORequest *) AudioRqPtr[i]);
  990.                     ok = i;
  991.         }    }    }
  992.         if (ok == -1)
  993.         {   for (i = 0; i <= 3; i++)
  994.                 if (receipter[i] < oldestreceipt)
  995.                 {   ok = i;
  996.                     oldestreceipt = receipter[i];
  997.                 }
  998.             AbortIO((struct IORequest *) AudioRqPtr[ok]);
  999.             WaitIO((struct IORequest *) AudioRqPtr[ok]);
  1000.         }
  1001.         eversent[ok] = TRUE;
  1002.         AudioRqPtr[ok]->ioa_Cycles              = 1;
  1003.         AudioRqPtr[ok]->ioa_Request.io_Command  = CMD_WRITE;
  1004.         AudioRqPtr[ok]->ioa_Request.io_Flags    = ADIOF_PERVOL;
  1005.         AudioRqPtr[ok]->ioa_Request.io_Unit     = (struct Unit *) (1 << ok);
  1006.         AudioRqPtr[ok]->ioa_Volume              = samp[index].volume;
  1007.         AudioRqPtr[ok]->ioa_Period              = (UWORD) samp[index].speed;
  1008.         AudioRqPtr[ok]->ioa_Request.io_Message.mn_ReplyPort
  1009.                                                 = AudioPortPtr[ok];
  1010.         AudioRqPtr[ok]->ioa_Data                = (UBYTE *) samp[index].base;
  1011.         AudioRqPtr[ok]->ioa_Length              = samp[index].length[samp[index].bank];
  1012.         BeginIO(AudioRqPtr[ok]);
  1013.         receipter[ok] = nextreceipt;
  1014. }   }
  1015.  
  1016. AGLOBAL void help(UBYTE type)
  1017. {   TEXT  title[10];
  1018.     SWORD x, y;
  1019.     UBYTE i;
  1020.  
  1021.     effect(FXHELP);
  1022.     if (type == ORB)
  1023.     {   strcpy(title, "Creatures");
  1024.         x = 112;
  1025.         y = 18 + ((CREATUREHELPS + 1) * 10); // 128 currently
  1026.     } else
  1027.     {   strcpy(title, "Objects");
  1028.         x = 640;
  1029.         y = 28 + ((LASTOBJECT / 2) * 10);
  1030.     }
  1031.     if (!(HelpWindowPtr = (struct Window *) OpenWindowTags(NULL,
  1032.     WA_Left,          (SCREENXPIXEL / 2) - (x / 2),
  1033.     WA_Top,           (SCREENYPIXEL / 2) - (y / 2),
  1034.     WA_Width,         x,
  1035.     WA_Height,        y,
  1036.     WA_IDCMP,         IDCMP_CLOSEWINDOW | IDCMP_RAWKEY | IDCMP_MOUSEBUTTONS,
  1037.     WA_Title,         title,
  1038.     WA_Gadgets,       NULL,
  1039.     WA_CustomScreen,  ScreenPtr,
  1040.     WA_DragBar,       TRUE,
  1041.     WA_CloseGadget,   TRUE,
  1042.     WA_NoCareRefresh, TRUE,
  1043.     WA_Activate,      TRUE,
  1044.     TAG_DONE)))
  1045.     {   say("Can't open help window!", RED);
  1046.         anykey(TRUE);
  1047.     } else
  1048.     {   SetAPen(HelpWindowPtr->RPort, WHITE);
  1049.         if (type == ORB)
  1050.         {   for (i = 0; i <= CREATUREHELPS; i++)
  1051.             {   Image.ImageData = ImageData[creaturehelp[i].image];
  1052.                 DrawImage
  1053.                 (   HelpWindowPtr->RPort,
  1054.                     &Image,
  1055.                     10,
  1056.                     15 + (i * 10)
  1057.                 );
  1058.                 Move(HelpWindowPtr->RPort, 26, 20 + (i * 10));
  1059.                 Text(HelpWindowPtr->RPort, creaturehelp[i].desc, strlen(creaturehelp[i].desc));
  1060.         }   }
  1061.         else /* assumes (type == AFFIXER) */
  1062.         {   for (i = 0; i <= LASTOBJECT; i++)
  1063.             {   Image.ImageData = ImageData[i];
  1064.                 if (i <= LASTOBJECT / 2)
  1065.                 {   Move(HelpWindowPtr->RPort, 26, 20 + (i * 10));
  1066.                     DrawImage
  1067.                     (   HelpWindowPtr->RPort,
  1068.                         &Image,
  1069.                         10,
  1070.                         15 + (i * 10)
  1071.                     );
  1072.                 } else
  1073.                 {   Move(HelpWindowPtr->RPort, 354, 10 + ((i - (LASTOBJECT / 2)) * 10));
  1074.                     DrawImage
  1075.                     (   HelpWindowPtr->RPort,
  1076.                         &Image,
  1077.                         338,
  1078.                         5 + ((i - (LASTOBJECT / 2)) * 10)
  1079.                     );
  1080.                 }
  1081.                 Text(HelpWindowPtr->RPort, objectdesc[i], strlen(objectdesc[i]));
  1082.         }   }
  1083.         helploop(type);
  1084. }   }
  1085.  
  1086. AGLOBAL void helpmanual(void)
  1087. {
  1088. /*  struct NewAmigaGuide NAG = {NULL};
  1089.     AMIGAGUIDECONTEXT Handle;
  1090.  
  1091.     if (!(AmigaGuideBase = (struct AmigaGuideBase *) OpenLibrary("amigaguide.library", 0L)))
  1092.     {   say("Can't open amigaguide.library!", RED);
  1093.         anykey(TRUE);
  1094.         return;
  1095.     }
  1096.  
  1097.     // Fill in the NewAmigaGuide structure
  1098.     NAG.nag_Name = "PROGDIR:WormWars.guide";
  1099.     NAG.nag_Node = NULL;
  1100.     /* If you wanted a specific node of the AmigaGuide, you could set
  1101.     nag_Node to, for example, "Edit". */
  1102.     NAG.nag_Screen = ScreenPtr; // or NULL for default public screen
  1103.  
  1104.     // Open the AmigaGuide client
  1105.     if (Handle = OpenAmigaGuide(&NAG, TAG_DONE))
  1106.     {   /* if you opened it on the default public screen, add this line:
  1107.         ScreenToFront(ScreenPtr); */
  1108.         // Close the AmigaGuide client
  1109.         CloseAmigaGuide(Handle);
  1110.     } else
  1111.     {   /* if you want to know the reason, add this line:
  1112.         LONG rc = IoErr(); */
  1113.         DisplayBeep(ScreenPtr);
  1114.     }
  1115.     CloseLibrary((struct Library *) AmigaGuideBase); */
  1116.  
  1117.     if (SystemTagList("SYS:Utilities/MultiView >NIL: WormWars.guide", TAG_DONE) == -1)
  1118.         DisplayBeep(ScreenPtr);
  1119.     else ScreenToFront(ScreenPtr);
  1120. }
  1121.  
  1122. AGLOBAL void helpabout(void)
  1123. {       SBYTE line;
  1124.         SLONG projectval;
  1125.         TEXT  projectstring[7], ks[5] = "    ", wb[5] = "    ";
  1126.         ULONG ksval = SysBase->LibNode.lib_Version,
  1127.               wbval = IconBase->lib_Version;
  1128.  
  1129.         effect(FXHELP);
  1130.     if (!(HelpWindowPtr = (struct Window *) OpenWindowTags(NULL,
  1131.     WA_Left,            (SCREENXPIXEL / 2) - (ABOUTXPIXEL / 2),
  1132.     WA_Top,                (SCREENYPIXEL / 2) - (ABOUTYPIXEL / 2),
  1133.     WA_Width,            ABOUTXPIXEL,
  1134.     WA_Height,            ABOUTYPIXEL,
  1135.     WA_IDCMP,            IDCMP_CLOSEWINDOW | IDCMP_RAWKEY,
  1136.     WA_Title,            "About Worm Wars",
  1137.     WA_Gadgets,            NULL,
  1138.     WA_CustomScreen,    ScreenPtr,
  1139.     WA_DragBar,            TRUE,
  1140.     WA_CloseGadget,        TRUE,
  1141.     WA_NoCareRefresh,    TRUE,
  1142.     WA_Activate,        TRUE,
  1143.     TAG_DONE)))
  1144.     {    say("Can't open About... window!", RED);
  1145.         anykey(TRUE);
  1146.     } else
  1147.     {    /* calculate project size */
  1148.         
  1149.         projectval =
  1150.                   10                                 // header
  1151.                 + ((HISCORES + 1) * HISCORESIZE)     // high scores
  1152.                 + ((levels + 1) * (8 + (LEVELSIZE))) // level data
  1153.                 + strlen(VERSION);                   // version string
  1154.  
  1155.                 switch(ksval)
  1156.                 {
  1157.                 case 37:
  1158.                         strcpy(ks, "2.04");
  1159.                 break;
  1160.                 case 38:
  1161.                         strcpy(ks, "2.1 ");
  1162.                 break;
  1163.                 case 39:
  1164.                         strcpy(ks, "3.0 ");
  1165.                 break;
  1166.                 case 40:
  1167.                         strcpy(ks, "3.1 ");
  1168.                 break;
  1169.                 default:
  1170.                         strcpy(ks, "4.0+");
  1171.                 break;
  1172.                 }
  1173.  
  1174.                 switch(wbval)
  1175.                 {
  1176.                 case 37:
  1177.                         strcpy(wb, "2.04");
  1178.                 break;
  1179.                 case 38:
  1180.                         strcpy(wb, "2.1 ");
  1181.                 break;
  1182.                 case 39:
  1183.                         strcpy(wb, "3.0 ");
  1184.                 break;
  1185.                 case 40:
  1186.                 case 41:
  1187.                 case 42:
  1188.                 case 43:
  1189.                     strcpy(wb, "3.1 ");
  1190.                 break;
  1191.                 case 44:
  1192.                     strcpy(wb, "3.5 ");
  1193.                 break;
  1194.                 case 45:
  1195.                     strcpy(wb, "3.9 ");
  1196.                 break;
  1197.                 default:
  1198.                     strcpy(wb, "4.0+");
  1199.                 break;
  1200.                 }
  1201.                 
  1202.         SetAPen(HelpWindowPtr->RPort, worm[rand() % 4].colour);
  1203.         RectFill(HelpWindowPtr->RPort, 8, 13, ABOUTXPIXEL - 11, ABOUTYPIXEL - 6);
  1204.         SetAPen(HelpWindowPtr->RPort, ABOUTSHADOW);
  1205.         Move(HelpWindowPtr->RPort, 7, ABOUTYPIXEL - 5);
  1206.         Draw(HelpWindowPtr->RPort, 7, 12);
  1207.         Draw(HelpWindowPtr->RPort, ABOUTXPIXEL - 10, 12);
  1208.         SetAPen(HelpWindowPtr->RPort, ABOUTSHINE);
  1209.         Draw(HelpWindowPtr->RPort, ABOUTXPIXEL - 10, ABOUTYPIXEL - 5);
  1210.         Draw(HelpWindowPtr->RPort, 8, ABOUTYPIXEL - 5);
  1211.         SetAPen(HelpWindowPtr->RPort, BLACK);
  1212.         SetDrMd(HelpWindowPtr->RPort, JAM1);
  1213.         for (line = 0; line <= ABOUTLINES; line++)
  1214.                 {   Move(HelpWindowPtr->RPort, about[line].x, about[line].y);
  1215.                     Text(HelpWindowPtr->RPort, about[line].text, (SBYTE) strlen(about[line].text));
  1216.         }
  1217.         stcl_d(projectstring, projectval);
  1218.                 align(projectstring, 6, ' ');
  1219.         Move(HelpWindowPtr->RPort, PROJECTX, PROJECTY);
  1220.                 Text(HelpWindowPtr->RPort, projectstring, 6);
  1221.                 Move(HelpWindowPtr->RPort, KICKSTARTX, KICKSTARTY);
  1222.                 Text(HelpWindowPtr->RPort, ks, 4);
  1223.                 Move(HelpWindowPtr->RPort, WORKBENCHX, WORKBENCHY);
  1224.                 Text(HelpWindowPtr->RPort, wb, 4);
  1225.  
  1226.         DrawBevelBox(HelpWindowPtr->RPort, 18, 21, 47, 22, GT_VisualInfo, VisualInfoPtr);
  1227.         DrawImage(HelpWindowPtr->RPort, &About, 19, 22);
  1228.  
  1229.                 // boingball();
  1230.                 helploop(NOSQUARE);
  1231. }        }
  1232.  
  1233. AGLOBAL void helploop(UBYTE type)
  1234. {   ABOOL                done = FALSE;
  1235.     UBYTE                objx, objy;
  1236.     UWORD                code, qual;
  1237.     SWORD                mousex, mousey;
  1238.     ULONG                class;
  1239.     struct IntuiMessage* MsgPtr;
  1240.  
  1241.     while(!done)
  1242.     {   /* if (type == NOSQUARE)
  1243.             DrawGels();
  1244.         else */ Wait(1L << HelpWindowPtr->UserPort->mp_SigBit);
  1245.         while (MsgPtr = (struct IntuiMessage *) GetMsg(HelpWindowPtr->UserPort))
  1246.         {   class  = MsgPtr->Class;
  1247.             code   = MsgPtr->Code;
  1248.             qual   = MsgPtr->Qualifier;
  1249.             mousex = MsgPtr->MouseX;
  1250.             mousey = MsgPtr->MouseY;
  1251.             ReplyMsg((struct Message *) MsgPtr);
  1252.             switch(class)
  1253.             {
  1254.             case IDCMP_CLOSEWINDOW:
  1255.                 done = TRUE;
  1256.             break;
  1257.             case IDCMP_RAWKEY:
  1258.                 if (code == SPACEBAR || code == RETURN || code == ENTER || code == HELP)
  1259.                     done = TRUE;
  1260.                 elif (code == ESCAPE)
  1261.                 {   if (qual & IEQUALIFIER_LSHIFT || qual & IEQUALIFIER_RSHIFT)
  1262.                     {   if (verify())
  1263.                         {   CloseWindow(HelpWindowPtr);
  1264.                             cleanexit(EXIT_SUCCESS);
  1265.                    }   }
  1266.                    else done = TRUE;
  1267.                 }
  1268.             break;
  1269.             case IDCMP_MOUSEBUTTONS:
  1270.                 if (a == FIELDEDIT && type == AFFIXER)
  1271.                 {   objx = (mousex - 10) / 328; // which column
  1272.                     objy = (mousey - 13) / 10;  // which row
  1273.                     if ((objx == 0 || objx == 1) && (objy >= 0 && objy <= LASTOBJECT / 2))
  1274.                     {   if (objx)
  1275.                             setbrush(objy + 1 + (LASTOBJECT / 2));
  1276.                         else setbrush(objy);
  1277.                         done = TRUE;
  1278.                 }   }
  1279.             break;
  1280.             default:
  1281.             break;
  1282.     }   }   }
  1283.     /* if (type == NOSQUARE)
  1284.          unboingball(); */
  1285.     CloseWindow(HelpWindowPtr);
  1286.     clearkybd();
  1287. }
  1288.  
  1289. AGLOBAL void filedelete(void)
  1290. {   TEXT newpathname[255], temp1[81];
  1291.  
  1292.     if (AslRequestTags(ASLRqPtr, ASL_Hail, "Delete Fieldset", ASL_FuncFlags, FILF_PATGAD | FILF_SAVE, TAG_DONE) && *(ASLRqPtr->rf_File) != 0)
  1293.     {   strcpy(newpathname, ASLRqPtr->rf_Dir);
  1294.         AddPart(newpathname, ASLRqPtr->rf_File, 254);
  1295.         if (DeleteFile(newpathname))
  1296.         {   strcpy(temp1, "Deleted ");
  1297.             strcat(temp1, newpathname);
  1298.             strcat(temp1, ".");
  1299.         } else
  1300.         {   strcpy(temp1, "Couldn't delete ");
  1301.             strcat(temp1, newpathname);
  1302.             strcat(temp1, "!");
  1303.         }
  1304.         say(temp1, WHITE);
  1305. }   }
  1306.  
  1307. AGLOBAL void fileopen(ABOOL revert)
  1308. {   TEXT temp1[81] = {"Opened "}, temp2[3], newpathname[255];
  1309.  
  1310.     strcpy(newpathname, pathname);
  1311.     if (revert || (AslRequestTags(ASLRqPtr, ASL_Hail, "Open Fieldset", ASL_FuncFlags, FILF_PATGAD, TAG_DONE) && *(ASLRqPtr->rf_File)))
  1312.     {   if (!revert)
  1313.         {   strcpy(newpathname, ASLRqPtr->rf_Dir);
  1314.             AddPart(newpathname, ASLRqPtr->rf_File, 254);
  1315.         }
  1316.         if (!loadfields(newpathname))
  1317.         {   strcpy(pathname, newpathname);
  1318.             strcat(temp1, pathname);
  1319.             strcat(temp1, " (");
  1320.             stci_d(temp2, levels);
  1321.             strcat(temp1, temp2);
  1322.             strcat(temp1, " levels).");
  1323.             say(temp1, WHITE);
  1324.             if (a == FIELDEDIT)
  1325.                 turborender();
  1326.             else hiscores();
  1327.         } else
  1328.         {   strcpy(temp1, "Couldn't open ");
  1329.             strcat(temp1, newpathname);
  1330.             strcat(temp1, "!");
  1331.             say(temp1, WHITE);
  1332.     }   }
  1333.     if (a == GAMEOVER)
  1334.         anykey(TRUE);
  1335. }
  1336.  
  1337. AGLOBAL void filesaveas(ABOOL flag)
  1338. {    ABOOL    cont = TRUE;
  1339.     TEXT    newpathname[255], temp1[SAYLIMIT + 1], temp2[3];
  1340.         struct DiskObject* InfoHandle;
  1341.  
  1342.     /* flag is TRUE for 'save as...', FALSE for 'save'. */
  1343.  
  1344.     strcpy(newpathname, pathname);
  1345.     if (flag)
  1346.         if (AslRequestTags(ASLRqPtr, ASL_Hail, "Save Fieldset", ASL_FuncFlags, FILF_PATGAD | FILF_SAVE, TAG_DONE) && *(ASLRqPtr->rf_File) != 0)
  1347.         {   strcpy(newpathname, ASLRqPtr->rf_Dir);
  1348.             AddPart(newpathname, ASLRqPtr->rf_File, 254);
  1349.         } else cont = FALSE;
  1350.     if (cont)
  1351.     {   matchteleports();
  1352.         strcpy(temp1, "Saving ");
  1353.     strcat(temp1, newpathname);
  1354.     strcat(temp1, "...");
  1355.     say(temp1, WHITE);
  1356.     if (savefields(newpathname))
  1357.         {   strcpy(pathname, newpathname);
  1358.             if (icons)
  1359.             {   InfoHandle = GetDiskObjectNew(DEFAULTSET);
  1360.                 InfoHandle->do_CurrentX = NO_ICON_POSITION;
  1361.                 InfoHandle->do_CurrentY = NO_ICON_POSITION;
  1362.                 if (!PutDiskObject(pathname, InfoHandle))
  1363.                 {   say("Couldn't write .info file!", RED);
  1364.                     anykey(TRUE);
  1365.             }   }
  1366.             strcpy(temp1, "Saved ");
  1367.             strcat(temp1, pathname);
  1368.             strcat(temp1, " (");
  1369.             stci_d(temp2, levels);
  1370.             strcat(temp1, temp2);
  1371.             strcat(temp1, " levels).");
  1372.         } else
  1373.         {   strcpy(temp1, "Couldn't save ");
  1374.             strcat(temp1, newpathname);
  1375.             strcat(temp1, "!");
  1376.         }
  1377.         say(temp1, WHITE);
  1378.         if (a == GAMEOVER)
  1379.             anykey(TRUE);
  1380. }   }
  1381.  
  1382. MODULE void freefx(void)
  1383. {   SBYTE i;
  1384.  
  1385.     stopfx();
  1386.     if (!AudioClosed)
  1387.     {   CloseDevice((struct IORequest *) AudioRqPtr[0]);
  1388.         AudioClosed = TRUE;
  1389.     }
  1390.     for (i = 0; i <= 3; i++)
  1391.     {   if (AudioRqPtr[i])
  1392.         {   DeleteIORequest(AudioRqPtr[i]);
  1393.             AudioRqPtr[i] = NULL;
  1394.         }
  1395.         if (AudioPortPtr[i])
  1396.         {   // if we ReplyMsg() first, that causes crashes
  1397.             DeleteMsgPort(AudioPortPtr[i]);
  1398.             AudioPortPtr[i] = NULL;
  1399.     }   }
  1400.     if (fbase)
  1401.     {   FreeMem(fbase, fsize);
  1402.         fbase = NULL;
  1403.     }
  1404.     if (FilePtr)
  1405.     {   Close(FilePtr);
  1406.         FilePtr = NULL;
  1407. }   }
  1408.  
  1409. void gameinput(void)
  1410. {   ABOOL                done;
  1411.     UWORD                code, qual;
  1412.     ULONG                class;
  1413.     struct IntuiMessage* MsgPtr;
  1414.     SBYTE                keyplayer;
  1415.     UBYTE                which;
  1416.  
  1417.     for (which = 0; which <= NUMKEYS; which++)
  1418.         key[which].down = FALSE;
  1419.  
  1420. /* keyboard */
  1421.  
  1422. while (MsgPtr = (struct IntuiMessage *) GetMsg(MainWindowPtr->UserPort))
  1423. {   class = MsgPtr->Class;
  1424.     code  = MsgPtr->Code;
  1425.     qual  = MsgPtr->Qualifier;
  1426.     ReplyMsg((struct Message *) MsgPtr);
  1427.     if (class == IDCMP_RAWKEY && (!(qual & IEQUALIFIER_REPEAT)))
  1428.     {   if (code < KEYUP)
  1429.         {   switch(code)
  1430.             {
  1431.             case M:
  1432.                 toggle(M);
  1433.             break;
  1434.                 case F:
  1435.                 toggle(F);
  1436.             break;
  1437.             case P:
  1438.                 clearkybd();
  1439.                 say("Paused...press P to unpause", WHITE);
  1440.                 pausetimer();
  1441.                 done = FALSE;
  1442.                 while (!done)
  1443.                 {   Wait(1L << MainWindowPtr->UserPort->mp_SigBit);
  1444.                     while (MsgPtr = (struct IntuiMessage *) GetMsg(MainWindowPtr->UserPort))
  1445.                     {   class = MsgPtr->Class;
  1446.                         code  = MsgPtr->Code;
  1447.                         qual  = MsgPtr->Qualifier;
  1448.                         ReplyMsg((struct Message *) MsgPtr);
  1449.                         if (class == IDCMP_RAWKEY && (!(qual & IEQUALIFIER_REPEAT)))
  1450.                         {   if (code == ESCAPE)
  1451.                             {   if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  1452.                                 {   if (verify())
  1453.                                         cleanexit(EXIT_SUCCESS);
  1454.                                 } else
  1455.                                 {   a = GAMEOVER;
  1456.                                     done = TRUE;
  1457.                                     worm[0].lives = worm[1].lives = worm[2].lives = worm[3].lives = 0;
  1458.                             }   }
  1459.                             elif (code == M)
  1460.                                 toggle(M);
  1461.                             elif (code == F)
  1462.                                 toggle(F);
  1463.                             elif (code == P)
  1464.                             {   say("Unpaused", WHITE);
  1465.                                 done = TRUE;
  1466.                 }   }   }   }
  1467.                 unpausetimer();
  1468.             break;
  1469.             case ESCAPE:
  1470.                 if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  1471.                 {   if (verify())
  1472.                         cleanexit(EXIT_SUCCESS);
  1473.                 } else
  1474.                 {   a = GAMEOVER;
  1475.                     worm[0].lives = worm[1].lives = worm[2].lives = worm[3].lives = 0;
  1476.                 }
  1477.             break;
  1478.             default:
  1479.                for (which = 0; which <= NUMKEYS; which++)
  1480.                    if (code == key[which].scancode)
  1481.                        key[which].down = TRUE;
  1482.             break;
  1483.     }   }   }
  1484.     elif (class == IDCMP_CLOSEWINDOW)
  1485.         cleanexit(EXIT_SUCCESS);
  1486.     elif (class == IDCMP_REFRESHWINDOW)
  1487.     {   GT_BeginRefresh(MainWindowPtr);
  1488.         GT_EndRefresh(MainWindowPtr, TRUE);
  1489. }   }
  1490.  
  1491. /* Received but ignored: IDCMP_ACTIVEWINDOW, IDCMP_MOUSEBUTTONS and
  1492. IDCMP_INTUITICKS. */
  1493.  
  1494. for (which = 0; which <= NUMKEYS; which++)
  1495. {   if (key[which].down)
  1496.     {   if (key[which].special == ONEHUMAN)
  1497.         {   if
  1498.             (   (worm[0].control == KEYBOARD && worm[1].control != KEYBOARD)
  1499.             )
  1500.                 wormqueue(0, key[which].deltax, key[which].deltay);
  1501.             elif
  1502.             (   (worm[1].control == KEYBOARD && worm[0].control != KEYBOARD)
  1503.             )
  1504.                 wormqueue(1, key[which].deltax, key[which].deltay);
  1505.         } elif (key[which].special == MOVE || key[which].special == AMMO)
  1506.         {   if (worm[key[which].player].control == KEYBOARD)
  1507.                 keyplayer = key[which].player;
  1508.             elif
  1509.             (   key[which].player == 1
  1510.              && (worm[0].control == KEYBOARD && worm[1].control != KEYBOARD)
  1511.             )
  1512.                 keyplayer = 0;
  1513.             elif
  1514.             (   key[which].player == 0
  1515.              && (worm[1].control == KEYBOARD && worm[0].control != KEYBOARD)
  1516.             )
  1517.                 keyplayer = 1;
  1518.             else keyplayer = -1;
  1519.             if (keyplayer != -1)
  1520.             {   wormqueue(keyplayer, key[which].deltax, key[which].deltay);
  1521.         }   }
  1522.         elif (worm[1].lives) /* assumes key[which].special == TRAINER */
  1523.             train(key[which].scancode);
  1524. }   }
  1525.  
  1526. /* joystick `2' */
  1527.  
  1528. if (worm[2].control == JOYSTICK)
  1529.     while (GetMsg(JoyPortPtr))
  1530.     {   if (GameEvent.ie_position.ie_xy.ie_x != 0 || GameEvent.ie_position.ie_xy.ie_y != 0)
  1531.             wormqueue(2, GameEvent.ie_position.ie_xy.ie_x, GameEvent.ie_position.ie_xy.ie_y);
  1532.         if (GameEvent.ie_Code == IECODE_LBUTTON)
  1533.             wormqueue(2, 0, 0);
  1534.         sendreadrequest();
  1535. }   }
  1536.  
  1537. void hiscores(void)
  1538. {   SBYTE which;
  1539.     TEXT  tempstring[NAMELENGTH + 1];
  1540.  
  1541. /* render hiscores
  1542.  
  1543. #################################################### # = shadow
  1544. #   #   #   #                      #     #         % % = shine
  1545. #   #   #   #                      #     #         %
  1546. #   #   #   #                      #     #         %
  1547. #   #   #   #                      #     #         %
  1548. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
  1549.  
  1550. SetDrMd(MainWindowPtr->RPort, JAM1);
  1551. for (which = 0; which <= HISCORES; which++)
  1552. {    if (hiscore[which].player == -1)
  1553.         SetAPen(MainWindowPtr->RPort, LIGHTGREY);
  1554.     else SetAPen(MainWindowPtr->RPort, worm[hiscore[which].player].colour);
  1555.         RectFill(MainWindowPtr->RPort, 104, TSOFFSET + 1 + (which * HISCOREDISTANCE), 536, TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1556.     if (hiscore[which].player == 0)
  1557.         SetAPen(MainWindowPtr->RPort, DARKGREEN);
  1558.     else if (hiscore[which].player == 1)
  1559.         SetAPen(MainWindowPtr->RPort, DARKRED);
  1560.     else if (hiscore[which].player == 2)
  1561.         SetAPen(MainWindowPtr->RPort, DARKBLUE);
  1562.     else if (hiscore[which].player == 3)
  1563.         SetAPen(MainWindowPtr->RPort, DARKYELLOW);
  1564.     else SetAPen(MainWindowPtr->RPort, DARKGREY);
  1565.         Move(MainWindowPtr->RPort, 103, TSOFFSET + 11 + (which * HISCOREDISTANCE));
  1566.         Draw(MainWindowPtr->RPort, 103, TSOFFSET +      (which * HISCOREDISTANCE));
  1567.         Draw(MainWindowPtr->RPort, 537, TSOFFSET +      (which * HISCOREDISTANCE));
  1568.  
  1569.     if (hiscore[which].player != -1)
  1570.     {    /* divider bars */
  1571.     
  1572.                 Move(MainWindowPtr->RPort, 182 - 55, TSOFFSET +      (which * HISCOREDISTANCE));
  1573.                 Draw(MainWindowPtr->RPort, 182 - 55, TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1574.                 Move(MainWindowPtr->RPort, 254 - 55, TSOFFSET +      (which * HISCOREDISTANCE));
  1575.                 Draw(MainWindowPtr->RPort, 254 - 55, TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1576.                 Move(MainWindowPtr->RPort, 290 - 55, TSOFFSET +      (which * HISCOREDISTANCE));
  1577.                 Draw(MainWindowPtr->RPort, 290 - 55, TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1578.                 Move(MainWindowPtr->RPort, 416,      TSOFFSET +      (which * HISCOREDISTANCE));
  1579.                 Draw(MainWindowPtr->RPort, 416,      TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1580.                 Move(MainWindowPtr->RPort, 464,      TSOFFSET +      (which * HISCOREDISTANCE));
  1581.                 Draw(MainWindowPtr->RPort, 464,      TSOFFSET + 10 + (which * HISCOREDISTANCE));
  1582.     }
  1583.     
  1584.     SetAPen(MainWindowPtr->RPort, WHITE);
  1585.         Move(MainWindowPtr->RPort, 159 - 55, TSOFFSET + 11 + (which * HISCOREDISTANCE));
  1586.         Draw(MainWindowPtr->RPort, 482 + 55, TSOFFSET + 11 + (which * HISCOREDISTANCE));
  1587.         Draw(MainWindowPtr->RPort, 482 + 55, TSOFFSET +  1 + (which * HISCOREDISTANCE));
  1588.     SetAPen(MainWindowPtr->RPort, BLACK);
  1589.  
  1590.     if (hiscore[which].player != -1)
  1591.     {    stci_d(tempstring, which + 1);
  1592.         tempstring[1] = '.';
  1593.                 Move(MainWindowPtr->RPort, 161 - 55, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1594.         Text(MainWindowPtr->RPort, tempstring, 2);
  1595.         stci_d(tempstring, hiscore[which].score);
  1596.         align(tempstring, 7, ' ');
  1597.                 Move(MainWindowPtr->RPort, 193 - 55, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1598.         Text(MainWindowPtr->RPort, tempstring, 7);
  1599.         if (hiscore[which].level == -1)
  1600.             strcpy(tempstring, "All");
  1601.         else
  1602.         {    stci_d(tempstring, hiscore[which].level);
  1603.             align(tempstring, 3, ' ');
  1604.         }
  1605.                 Move(MainWindowPtr->RPort, 206, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1606.         Text(MainWindowPtr->RPort, tempstring, 3);
  1607.                 Move(MainWindowPtr->RPort, 241, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1608.         Text(MainWindowPtr->RPort, hiscore[which].name, strlen(hiscore[which].name));
  1609.                 Move(MainWindowPtr->RPort, 418, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1610.         Text(MainWindowPtr->RPort, hiscore[which].time, strlen(hiscore[which].time));
  1611.                 Move(MainWindowPtr->RPort, 466, TSOFFSET + 8 + (which * HISCOREDISTANCE));
  1612.         Text(MainWindowPtr->RPort, hiscore[which].date, strlen(hiscore[which].date));
  1613. }    }
  1614. SetDrMd(MainWindowPtr->RPort, JAM2);
  1615. }
  1616.  
  1617. void hiscorenames(void)
  1618. {
  1619. ULONG                    class;
  1620. ABOOL                    done;
  1621. SBYTE                    which;
  1622. struct IntuiMessage*    MsgPtr;
  1623.  
  1624. for (which = 0; which <= HISCORES; which++)
  1625.     if (hiscore[which].fresh)
  1626.     {    GT_SetGadgetAttrs(StringGadgetPtr[which], MainWindowPtr, NULL, GA_Disabled, FALSE, GTST_String, worm[hiscore[which].player].name, TAG_DONE);
  1627.         ActivateGadget(StringGadgetPtr[which], MainWindowPtr, NULL);
  1628.         done = FALSE;
  1629.         while (!done)
  1630.         {    while (MsgPtr = (struct IntuiMessage *) GT_GetIMsg(MainWindowPtr->UserPort))
  1631.             {    class = MsgPtr->Class;
  1632.                 GT_ReplyIMsg(MsgPtr);
  1633.                 if (class == IDCMP_GADGETUP)
  1634.                     done = TRUE;
  1635.                 else if (class == IDCMP_MOUSEBUTTONS)
  1636.                     ActivateGadget(StringGadgetPtr[which], MainWindowPtr, NULL);
  1637.                 else if (class == IDCMP_REFRESHWINDOW)
  1638.                 {    GT_BeginRefresh(MainWindowPtr);
  1639.                     GT_EndRefresh(MainWindowPtr, TRUE);
  1640.         }    }    }
  1641.         GT_SetGadgetAttrs(StringGadgetPtr[which], MainWindowPtr, NULL, GA_Disabled, TRUE, TAG_DONE);
  1642.                 effect(FXAPPLAUSE);
  1643.         strcpy(hiscore[which].name, ((struct StringInfo *) (StringGadgetPtr[which]->SpecialInfo))->Buffer);
  1644.         if (hiscore[which].name[0] >= 97 && hiscore[which].name[0] <= 123)
  1645.             hiscore[which].name[0] -= 32;
  1646.         strcpy(worm[hiscore[which].player].name, hiscore[which].name);
  1647.         hiscore[which].fresh = FALSE;
  1648.         hiscores();
  1649. }    }
  1650.  
  1651. MODULE ABOOL beginfx(void)
  1652. {           SBYTE    i;
  1653.     PERSIST    UBYTE    chan[]    = {15};
  1654.  
  1655.     for (i = 0; i <= 3; i++)
  1656.     {   eversent[i] = FALSE;
  1657.         if (!(AudioPortPtr[i] = (struct MsgPort *) CreateMsgPort()))
  1658.         {   freefx();
  1659.             draw(MUSICICON, ICONY, BLACKENED);
  1660.             mode = FALSE;
  1661.             say("No port for effects!", RED);
  1662.             anykey(TRUE);
  1663.             return FALSE;
  1664.         } else if (!(AudioRqPtr[i] = (struct IOAudio *) CreateIORequest(AudioPortPtr[i], sizeof(struct IOAudio))))
  1665.         {   freefx();
  1666.             draw(MUSICICON, ICONY, BLACKENED);
  1667.             mode = FALSE;
  1668.             say("No I/O memory for effects!", RED);
  1669.             anykey(TRUE);
  1670.             return FALSE;
  1671.     }   }
  1672.     AudioRqPtr[0]->ioa_Request.io_Message.mn_ReplyPort      = AudioPortPtr[0];
  1673.     AudioRqPtr[0]->ioa_Request.io_Message.mn_Node.ln_Pri    = 127;
  1674.     AudioRqPtr[0]->ioa_AllocKey                             = 0;
  1675.     AudioRqPtr[0]->ioa_Data                                 = chan;
  1676.     AudioRqPtr[0]->ioa_Length                               = 1;
  1677.     if (AudioClosed = OpenDevice(AUDIONAME, 0L, (struct IORequest *) AudioRqPtr[0], 0L))
  1678.     {   freefx();
  1679.         draw(MUSICICON, ICONY, BLACKENED);
  1680.         mode = FALSE;
  1681.         say("Can't allocate all channels for effects!", RED);
  1682.         anykey(TRUE);
  1683.         return FALSE;
  1684.     } else
  1685.     {   for (i = 1; i <= 3; i++)
  1686.             CopyMem(AudioRqPtr[0], AudioRqPtr[i], sizeof(struct IOAudio));
  1687.         return TRUE;
  1688. }   }
  1689.  
  1690. void resettime(void)
  1691. {   GetSysTime(StartValPtr);
  1692.     srand((UWORD) StartValPtr->tv_micro);
  1693. }
  1694.  
  1695. void rundown(SBYTE player)
  1696. {       ABOOL                done = FALSE, flag = FALSE;
  1697.     TEXT                 tempstring[6];
  1698.     SBYTE                i, j, x, y;
  1699.         UBYTE                multiply = worm[player].multi * players;
  1700.     UWORD                counter = 0, code, qual;
  1701.     ULONG                levelbonus, timebonus, tailbonus, class;
  1702.     struct IntuiMessage* MsgPtr;
  1703.  
  1704.     clearscreen();
  1705.     SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1706.  
  1707.     Image.ImageData = ImageData[TREASURE];
  1708.     DrawImage(MainWindowPtr->RPort, &Image, 201, 103);
  1709.     Move(MainWindowPtr->RPort, 218, 108);
  1710.     Text(MainWindowPtr->RPort, "Level Bonus:    ## x ##00 =", 27);
  1711.         stci_d(tempstring, multiply);
  1712.     align(tempstring, 2, ' ');
  1713.     Move(MainWindowPtr->RPort, 218 + (FONTX * 21), 108);
  1714.     Text(MainWindowPtr->RPort, tempstring, 2);
  1715.     stci_d(tempstring, level - 1);
  1716.     align(tempstring, 2, ' ');
  1717.     Move(MainWindowPtr->RPort, 218 + (FONTX * 16), 108);
  1718.     Text(MainWindowPtr->RPort, tempstring, 2);
  1719.  
  1720.     Image.ImageData = ImageData[CLOCK];
  1721.     DrawImage(MainWindowPtr->RPort, &Image, 201, 111);
  1722.     Move(MainWindowPtr->RPort, 218, 116);
  1723.     Text(MainWindowPtr->RPort, " Time Bonus: ##:## x  ##0 =", 27);
  1724.         stci_d(tempstring, multiply);
  1725.     align(tempstring, 2, ' ');
  1726.     Move(MainWindowPtr->RPort, 226 + (FONTX     * 21), 116);
  1727.     Text(MainWindowPtr->RPort, tempstring, 2);
  1728.     stci_d(tempstring, secondsleft / 60);
  1729.     align(tempstring, 2, ' ');
  1730.     Move(MainWindowPtr->RPort, 226 + (FONTX * 12), 116);
  1731.     Text(MainWindowPtr->RPort, tempstring, 2);
  1732.     stci_d(tempstring, secondsleft % 60);
  1733.     align(tempstring, 2, '0');
  1734.     Move(MainWindowPtr->RPort, 226 + (FONTX * 15), 116);
  1735.     Text(MainWindowPtr->RPort, tempstring, 2);
  1736.  
  1737.     Image.ImageData = ImageData[FIRSTTAIL + player];
  1738.     DrawImage(MainWindowPtr->RPort, &Image, 201, 119);
  1739.     Move(MainWindowPtr->RPort, 218, 124);
  1740.     Text(MainWindowPtr->RPort, " Tail Bonus:  #### x   ## =", 27);
  1741.         stci_d(tempstring, multiply);
  1742.     align(tempstring, 2, ' ');
  1743.         Move(MainWindowPtr->RPort, 226 + (FONTX * 22), 124);
  1744.         Text(MainWindowPtr->RPort, tempstring, 2);
  1745.     for (x = 0; x <= FIELDX; x++)
  1746.         for (y = 0; y <= FIELDY; y++)
  1747.             if (field[x][y] == FIRSTTAIL + player)
  1748.                 counter++;
  1749.     stci_d(tempstring, counter);
  1750.     align(tempstring, 4, ' ');
  1751.     Move(MainWindowPtr->RPort, 226 + (FONTX * 13), 124);
  1752.     Text(MainWindowPtr->RPort, tempstring, 4);
  1753.  
  1754.     levelbonus = (level - 1) * 100 * multiply;
  1755.     timebonus = secondsleft * 10 * multiply;
  1756.     tailbonus = counter * multiply;
  1757.  
  1758.     for (i = 0; i <= 3; i++)
  1759.         if (worm[i].control != NONE)
  1760.             for (j = 0; j <= LASTOBJECT; j++)
  1761.                 stat(i, j);
  1762.  
  1763.     while (!done)
  1764.     {   while (MsgPtr = (struct IntuiMessage *) GetMsg(MainWindowPtr->UserPort))
  1765.         {   class  = MsgPtr->Class;
  1766.         code   = MsgPtr->Code;
  1767.         qual   = MsgPtr->Qualifier;
  1768.         ReplyMsg((struct Message *) MsgPtr);
  1769.             if (class == IDCMP_RAWKEY && (!(qual & IEQUALIFIER_REPEAT)) && code < KEYUP)
  1770.                 done = TRUE;
  1771.         }
  1772.         if (done)
  1773.     {   wormscore
  1774.             (   player,
  1775.                 (levelbonus / multiply) +
  1776.                 ( timebonus / multiply) +
  1777.                 ( tailbonus / multiply)
  1778.             );
  1779.             levelbonus = timebonus = tailbonus = 0;
  1780.         }
  1781.         SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1782.         stci_d(tempstring, levelbonus);
  1783.         align(tempstring, 5, ' ');
  1784.         Move(MainWindowPtr->RPort, 218 + (FONTX * 27), 108);
  1785.         Text(MainWindowPtr->RPort, tempstring, 5);
  1786.  
  1787.         stci_d(tempstring, timebonus);
  1788.         align(tempstring, 5, ' ');
  1789.         Move(MainWindowPtr->RPort, 226 + (FONTX * 26), 116);
  1790.         Text(MainWindowPtr->RPort, tempstring, 5);
  1791.  
  1792.         stci_d(tempstring, tailbonus);
  1793.         align(tempstring, 5, ' ');
  1794.         Move(MainWindowPtr->RPort, 226 + (FONTX * 26), 124);
  1795.         Text(MainWindowPtr->RPort, tempstring, 5);
  1796.  
  1797.         done = TRUE;
  1798.         if (levelbonus)
  1799.         {   levelbonus -= multiply;
  1800.             wormscore(player, 1);
  1801.             done = FALSE;
  1802.         }
  1803.         if (timebonus)
  1804.         {   timebonus -= multiply;
  1805.             wormscore(player, 1);
  1806.             done = FALSE;
  1807.         }
  1808.         if (tailbonus)
  1809.         {   tailbonus -= multiply;
  1810.             wormscore(player, 1);
  1811.             done = FALSE;
  1812.         }
  1813.         effect(FXCLICK);
  1814.     }
  1815.     clearkybd();
  1816.     effect(FXRIFF);
  1817.     for (i = 0; i <= 3; i++)
  1818.         if (worm[i].control == KEYBOARD || worm[i].control == JOYSTICK)
  1819.         {   flag = TRUE;
  1820.             break;
  1821.         }
  1822.     waitasec();
  1823.     if (flag)
  1824.         anykey(FALSE);
  1825. }
  1826.  
  1827. void say(STRPTR sentence, COLOUR colour)
  1828. {   SBYTE length = (SBYTE) strlen(sentence);
  1829.  
  1830.     /* truncate text */
  1831.  
  1832.     if (length > SAYLIMIT)
  1833.         *(sentence + SAYLIMIT - 1) = *(sentence + SAYLIMIT - 2) = *(sentence + SAYLIMIT - 3) = '.';
  1834.  
  1835.     /* clear areas to left, right and top of text, respectively */
  1836.  
  1837.     SetAPen(MainWindowPtr->RPort, BLACK);
  1838.     RectFill
  1839.     (   MainWindowPtr->RPort,
  1840.         SAYSTARTXPIXEL,
  1841.         2,
  1842.         (SCREENXPIXEL / 2) - (length * FONTX / 2),
  1843.         10
  1844.     );
  1845.     RectFill
  1846.     (   MainWindowPtr->RPort,
  1847.         (SCREENXPIXEL / 2) + (length * FONTX / 2) + 1,
  1848.         2,
  1849.         SAYENDXPIXEL,
  1850.         10
  1851.     );
  1852.     Move(MainWindowPtr->RPort, (SCREENXPIXEL / 2) - (length * FONTX / 2), 2);
  1853.     Draw(MainWindowPtr->RPort, (SCREENXPIXEL / 2) + (length * FONTX / 2), 2);
  1854.  
  1855.     /* render shadow text */
  1856.     
  1857.     SetAPen(MainWindowPtr->RPort, MEDIUMGREY);
  1858.     Move(MainWindowPtr->RPort, (SCREENXPIXEL / 2) - (length * FONTX / 2) + 1, FONTY + 1);
  1859.     Text(MainWindowPtr->RPort, sentence, length);
  1860.     
  1861.     /* render actual text */
  1862.     
  1863.     SetDrMd(MainWindowPtr->RPort, JAM1);
  1864.     SetAPen(MainWindowPtr->RPort, colour);
  1865.     Move(MainWindowPtr->RPort, (SCREENXPIXEL / 2) - (length * FONTX / 2), FONTY);
  1866.     Text(MainWindowPtr->RPort, sentence, length);
  1867.     SetDrMd(MainWindowPtr->RPort, JAM2);
  1868. }
  1869.     
  1870. void sendreadrequest(void)
  1871. {   JoyRqPtr->io_Command = GPD_READEVENT;
  1872.     JoyRqPtr->io_Flags   = NULL;
  1873.     JoyRqPtr->io_Length  = sizeof(struct InputEvent);
  1874.     JoyRqPtr->io_Data    = (APTR) &GameEvent;
  1875.     SendIO((struct IORequest *) JoyRqPtr);
  1876. }
  1877.  
  1878. void stat(SBYTE player, SBYTE line)
  1879. {   ABOOL print = TRUE;
  1880.     SBYTE i, len, theline;
  1881.     TEXT  output[9];
  1882.  
  1883.     strcpy(output, "        "); /* 8 spaces */
  1884.     switch (line)
  1885.     {
  1886.     case BONUS:
  1887.         do
  1888.         {   worm[player].oldscore += (LIFEMODULO - (worm[player].oldscore % LIFEMODULO));
  1889.             if (worm[player].score >= worm[player].oldscore)
  1890.             {   worm[player].lives++;
  1891.                 stat(player, LIFE);
  1892.         }   }
  1893.         while (worm[player].score > worm[player].oldscore);
  1894.         worm[player].oldscore = worm[player].score;
  1895.         if (worm[player].multi > 1)
  1896.             SetAPen(MainWindowPtr->RPort, WHITE);
  1897.         else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1898.         stcl_d(output, worm[player].score);
  1899.         theline = 0;
  1900.         if (worm[player].score <= 999999)
  1901.         {   len = 6;
  1902.             if (player == 1 && iso && worm[1].score <= 99999)
  1903.                 align(output, 6, ' ');
  1904.             else
  1905.                 for (i = 0; i <= 8; i++)
  1906.                     if (!output[i])
  1907.                         output[i] = ' ';
  1908.         } else
  1909.         {   len = 8;
  1910.             for (i = 0; i <= 8; i++)
  1911.                 if (!output[i])
  1912.                     output[i] = ' ';
  1913.             // assert(worm[player].score <= 99999999);
  1914.         }
  1915.     break;
  1916.     case LIFE:
  1917.         if (worm[player].lives > STARTLIVES)
  1918.         {   SetAPen(MainWindowPtr->RPort, WHITE);
  1919.             if (worm[player].lives > LIVESLIMIT)
  1920.                 worm[player].lives = LIVESLIMIT;
  1921.         } else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1922.         stci_d(output, worm[player].lives);
  1923.         if (player == 1 && iso)
  1924.         {   len = 6;
  1925.             align(output, 6, ' ');
  1926.         } else
  1927.         {   len = 3;
  1928.             for (i = 0; i <= 2; i++)
  1929.                 if (!output[i])
  1930.                     output[i] = ' ';
  1931.         }
  1932.         theline = 1;
  1933.     break;
  1934.     case BIAS:
  1935.         if (worm[player].bias > 0)
  1936.         {   if (worm[player].bias > BIASLIMIT)
  1937.                 worm[player].bias = BIASLIMIT;
  1938.             SetAPen(MainWindowPtr->RPort, WHITE);
  1939.         } else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1940.         stci_d(output, worm[player].bias);
  1941.         for (i = 0; i <= 2; i++)
  1942.             if (!output[i])
  1943.                 output[i] = ' ';
  1944.         theline = 2;
  1945.         len = 3;
  1946.     break;
  1947.     case NITRO:
  1948.             if (worm[player].nitro)
  1949.                 SetAPen(MainWindowPtr->RPort, WHITE);
  1950.             else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1951.             if (worm[player].speed == FAST)
  1952.                 strcpy(output, "Fast  ");
  1953.             elif (worm[player].speed == NORMAL)
  1954.                 strcpy(output, "Normal");
  1955.             elif (worm[player].speed == SLOW)
  1956.                 strcpy(output, "Slow  ");
  1957.             else
  1958.             {   // assert(worm[player].speed == VERYSLOW);
  1959.                 strcpy(output, "V.Slow");
  1960.             }
  1961.             theline = 3;
  1962.             len = 6;
  1963.         break;
  1964.     case AMMO:
  1965.             if (worm[player].ammo)
  1966.             {   if (worm[player].ammo > AMMOLIMIT)
  1967.                     worm[player].ammo = AMMOLIMIT;
  1968.                 SetAPen(MainWindowPtr->RPort, WHITE);
  1969.             } else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1970.             stci_d(output, worm[player].ammo);
  1971.             for (i = 0; i <= 2; i++)
  1972.                 if (!output[i])
  1973.                     output[i] = ' ';
  1974.             if (iso)
  1975.                 theline = 4;
  1976.             else theline = 5;
  1977.             len = 3;
  1978.         break;
  1979.     case POWER:
  1980.             switch(worm[player].power)
  1981.             {
  1982.             case 0:
  1983.                 SetAPen(MainWindowPtr->RPort, worm[player].colour);
  1984.                 strcpy(output, "Single");
  1985.             break;
  1986.             case 2:
  1987.                 SetAPen(MainWindowPtr->RPort, WHITE);
  1988.                 strcpy(output, "Triple");
  1989.             break;
  1990.             case 4:
  1991.                 SetAPen(MainWindowPtr->RPort, WHITE);
  1992.                 strcpy(output, "Quint.");
  1993.             break;
  1994.             case 6:
  1995.                 SetAPen(MainWindowPtr->RPort, WHITE);
  1996.                 strcpy(output, "Sept. ");
  1997.             break;
  1998.             default:
  1999.             break;
  2000.             }
  2001.             if (iso)
  2002.                 theline = 5;
  2003.             else theline = 6;
  2004.             len = 6;
  2005.         break;
  2006.     case ARMOUR:
  2007.             if (worm[player].armour > MODELIMIT)
  2008.                 worm[player].armour = MODELIMIT;
  2009.             if (worm[player].mode == ARMOUR)
  2010.                 SetAPen(MainWindowPtr->RPort, WHITE);
  2011.             else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  2012.             stci_d(output, worm[player].armour);
  2013.             for (i = 0; i <= 2; i++)
  2014.                 if (!output[i])
  2015.                     output[i] = ' ';
  2016.             if (iso)
  2017.                 theline = 6;
  2018.             else theline = 8;
  2019.             len = 3;
  2020.     break;
  2021.     case TONGUE:
  2022.         if (worm[player].tongue > MODELIMIT)
  2023.             worm[player].tongue = MODELIMIT;
  2024.         if (worm[player].mode == TONGUE)
  2025.             SetAPen(MainWindowPtr->RPort, WHITE);
  2026.         else SetAPen(MainWindowPtr->RPort, worm[player].colour);
  2027.         stci_d(output, worm[player].tongue);
  2028.         for (i = 0; i <= 2; i++)
  2029.             if (!output[i])
  2030.                 output[i] = ' ';
  2031.         if (iso)
  2032.             theline = 7;
  2033.         else theline = 9;
  2034.         len = 3;
  2035.     break;
  2036.     default:
  2037.         print = FALSE;
  2038.         /* These next two lines are just to prevent spurious compiler
  2039.         warnings about possibly uninitialized variables */
  2040.         theline = 0;
  2041.         len = 0;
  2042.     break;
  2043.     }
  2044.  
  2045.     /* Sometimes stat() is called with a valid line, yet an invalid player.
  2046.     Defensive programming currently ensures that this is not a problem, but
  2047.     it would be better to fix the bug. */
  2048.  
  2049.     if (print)
  2050.     {   if (iso)
  2051.         {   switch(player)
  2052.             {
  2053.             case 0:
  2054.                 Move(MainWindowPtr->RPort, 0  ,   8 + (theline * FONTY));
  2055.             break;
  2056.             case 1:
  2057.                 Move(MainWindowPtr->RPort, 528, 189 + (theline * FONTY));
  2058.             break;
  2059.             case 2:
  2060.                 Move(MainWindowPtr->RPort, 66 ,   8 + (theline * FONTY));
  2061.             break;
  2062.             case 3:
  2063.                 Move(MainWindowPtr->RPort, 592, 189 + (theline * FONTY));
  2064.             break;
  2065.             default:
  2066.                 // assert(0);
  2067.             break;
  2068.         }   }
  2069.         else
  2070.         {   Move
  2071.             (   MainWindowPtr->RPort,
  2072.                 (FONTX * 3) + (worm[player].statx * ENDXPIXEL),
  2073.                 STARTYPIXEL + 6 + (theline * FONTY) + (worm[player].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2074.             );
  2075.         }
  2076.         Text(MainWindowPtr->RPort, output, len);
  2077. }   }
  2078.  
  2079. void stopfx(void)
  2080. {   SBYTE i;
  2081.  
  2082.     if (mode == FX)
  2083.         for (i = 0; i <= 3; i++)
  2084.             // CheckIO() returns NULL if complete, pointer if incomplete? (!)
  2085.             if (eversent[i] && (!(CheckIO((struct IORequest *) AudioRqPtr[i]))))
  2086.             {   AbortIO((struct IORequest *) AudioRqPtr[i]);
  2087.                 WaitIO((struct IORequest *) AudioRqPtr[i]);
  2088. }           }
  2089.  
  2090. void titlescreen(void)
  2091. {   SBYTE                lisa = 0, object = LASTOBJECT, player;
  2092.     SWORD                descx = FIRSTDESCX;
  2093.     ULONG                class;
  2094.     UWORD                code, qual;
  2095.     struct IntuiMessage* MsgPtr;
  2096.     struct Gadget*       WhichGadgetPtr;
  2097.     struct MenuItem*     ItemPtr;
  2098.  
  2099.     effect(FXLAUNCH);
  2100.     Forbid();
  2101.     MainWindowPtr->Flags &= ~WFLG_RMBTRAP;
  2102.     Permit();
  2103.     clearscreen();
  2104.  
  2105.     SetMenuStrip(MainWindowPtr, MenuPtr);
  2106.     for (player = 0; player <= 3; player++)
  2107.         GT_SetGadgetAttrs(CycleGadgetPtr[player], MainWindowPtr, NULL, GA_Disabled, FALSE, TAG_DONE);
  2108.     GT_SetGadgetAttrs(RandomGadgetPtr, MainWindowPtr, NULL, GA_Disabled, FALSE, TAG_DONE);
  2109.     GT_SetGadgetAttrs(CheckboxGadgetPtr, MainWindowPtr, NULL, GA_Disabled, FALSE, TAG_DONE);
  2110.  
  2111.     clearkybd();
  2112.     clearjoystick();
  2113.     hiscores();
  2114.     hiscorenames();
  2115.  
  2116.         SetDrMd(MainWindowPtr->RPort, JAM1);
  2117.         SetAPen(MainWindowPtr->RPort, BLACK);
  2118.         Move(MainWindowPtr->RPort, STARTXPIXEL + 9, TSOFFSET +  86);
  2119.         Text(MainWindowPtr->RPort, "Spacebar: Field Editor ", 23);
  2120.         Move(MainWindowPtr->RPort, STARTXPIXEL + 9, TSOFFSET +  98);
  2121.         Text(MainWindowPtr->RPort, "       F: Sound Effects", 23);
  2122.         Move(MainWindowPtr->RPort, STARTXPIXEL + 9, TSOFFSET + 110);
  2123.         Text(MainWindowPtr->RPort, "       M: Music        ", 23);
  2124.         SetAPen(MainWindowPtr->RPort, WHITE);
  2125.         Move(MainWindowPtr->RPort, STARTXPIXEL + 8, TSOFFSET +  85);
  2126.         Text(MainWindowPtr->RPort, "Spacebar: Field Editor ", 23);
  2127.         Move(MainWindowPtr->RPort, STARTXPIXEL + 8, TSOFFSET +  97);
  2128.         Text(MainWindowPtr->RPort, "       F: Sound Effects", 23);
  2129.         Move(MainWindowPtr->RPort, STARTXPIXEL + 8, TSOFFSET + 109);
  2130.         Text(MainWindowPtr->RPort, "       M: Music        ", 23);
  2131.         SetDrMd(MainWindowPtr->RPort, JAM2);
  2132.  
  2133.     DrawImage(MainWindowPtr->RPort, &Logo, 203, 22);
  2134.  
  2135.     do
  2136.     {    TimerRqPtr->tr_node.io_Command    = TR_ADDREQUEST;
  2137.                 TimerRqPtr->tr_time.tv_secs     = 0;
  2138.         TimerRqPtr->tr_time.tv_micro    = ANIMDELAY;
  2139.         SendIO((struct IORequest *) TimerRqPtr);
  2140.  
  2141.         if (descx == FIRSTDESCX)
  2142.         {    if (++object > LASTOBJECT)
  2143.                 object = 0;
  2144.             say(objectdesc[object], WHITE);
  2145.             Image.ImageData = ImageData[object];
  2146.             DrawImage(MainWindowPtr->RPort, &Image, SECONDDESCX, DESCY);
  2147.         }
  2148.         SetAPen(MainWindowPtr->RPort, BLACK);
  2149.         Move(MainWindowPtr->RPort, descx - 1, DESCY - 1);
  2150.         Draw(MainWindowPtr->RPort, descx - 1, DESCY + 1 + SQUAREY);
  2151.         Move(MainWindowPtr->RPort, descx + 1 + SQUAREX, DESCY - 1);
  2152.         Draw(MainWindowPtr->RPort, descx + 1 + SQUAREX, DESCY + 1 + SQUAREY);
  2153.         Image.ImageData = ImageData[object];
  2154.         DrawImage(MainWindowPtr->RPort, &Image, descx, DESCY);
  2155.         if (++descx > SECONDDESCX)
  2156.             descx = FIRSTDESCX;
  2157.  
  2158.         while (MsgPtr = (struct IntuiMessage *) GT_GetIMsg(MainWindowPtr->UserPort))
  2159.                 {   WhichGadgetPtr = (struct Gadget *) MsgPtr->IAddress;
  2160.                     class = MsgPtr->Class;
  2161.                     code  = MsgPtr->Code;
  2162.                     qual  = MsgPtr->Qualifier;
  2163.                     GT_ReplyIMsg(MsgPtr);
  2164.                     switch (class)
  2165.                     {
  2166.                     case IDCMP_RAWKEY:
  2167.                         if (!(qual & IEQUALIFIER_REPEAT))
  2168.                             switch (code)
  2169.                             {
  2170.                             case F:
  2171.                                 toggle(F);
  2172.                             break;
  2173.                             case M:
  2174.                         toggle(M);
  2175.                         break;
  2176.                     case SPACEBAR:
  2177.                         effect(FXCLICK);
  2178.                         a = FIELDEDIT;
  2179.                         break;
  2180.                     case F1:
  2181.                     case ALPHAONE:
  2182.                     case NUMERICONE:
  2183.                         effect(FXCLICK);
  2184.                         if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  2185.                         {    if (--worm[0].control < 0)
  2186.                                                                 worm[0].control = 3;
  2187.                         } else
  2188.                                                 {       if (++worm[0].control > 3)
  2189.                                 worm[0].control = 0;
  2190.                         }
  2191.                         GT_SetGadgetAttrs(CycleGadgetPtr[0], MainWindowPtr, NULL, GTCY_Active, worm[0].control, TAG_DONE);
  2192.                         break;
  2193.                     case F2:
  2194.                     case ALPHATWO:
  2195.                     case NUMERICTWO:
  2196.                         effect(FXCLICK);
  2197.                         if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  2198.                         {    if (--worm[1].control < 0)
  2199.                                                                 worm[1].control = 3;
  2200.                         } else
  2201.                                                 {       if (++worm[1].control > 3)
  2202.                                 worm[1].control = 0;
  2203.                         }
  2204.                         GT_SetGadgetAttrs(CycleGadgetPtr[1], MainWindowPtr, NULL, GTCY_Active, worm[1].control, TAG_DONE);
  2205.                     break;
  2206.                     case F3:
  2207.                     case ALPHATHREE:
  2208.                     case NUMERICTHREE:
  2209.                         effect(FXCLICK);
  2210.                         if (joy)
  2211.                         {    if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  2212.                             {    if (--worm[2].control < 0)
  2213.                                     worm[2].control = 2;
  2214.                             } else
  2215.                             {    if (++worm[2].control > 2)
  2216.                                     worm[2].control = 0;
  2217.                         }    }
  2218.                         else if (worm[2].control == NONE)
  2219.                             worm[2].control = AMIGA;
  2220.                         else
  2221.                             worm[2].control = NONE;
  2222.                         GT_SetGadgetAttrs(CycleGadgetPtr[2], MainWindowPtr, NULL, GTCY_Active, worm[2].control, TAG_DONE);
  2223.                     break;
  2224.                     case F4:
  2225.                     case ALPHAFOUR:
  2226.                     case NUMERICFOUR:
  2227.                         effect(FXCLICK);
  2228.                         if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  2229.                         {    if (--worm[3].control < 0)
  2230.                                 worm[3].control = 2;
  2231.                         } else
  2232.                         {    if (++worm[3].control > 2)
  2233.                                 worm[3].control = 0;
  2234.                         }
  2235.                         GT_SetGadgetAttrs(CycleGadgetPtr[3], MainWindowPtr, NULL, GTCY_Active, worm[3].control, TAG_DONE);
  2236.                     break;
  2237.                     case RETURN:
  2238.                     case ENTER:
  2239.                         a = PLAYGAME;
  2240.                         break;
  2241.                     case I:
  2242.                         effect(FXCLICK);
  2243.                         iso = !iso;
  2244.                         GT_SetGadgetAttrs(CheckboxGadgetPtr, MainWindowPtr, NULL, GTCB_Checked, iso, TAG_DONE);
  2245.                         break;
  2246.                                         case S:
  2247.                                             effect(FXCLICK);
  2248.                                             randomflag = !randomflag;
  2249.                                             GT_SetGadgetAttrs(RandomGadgetPtr, MainWindowPtr, NULL, GTCB_Checked, randomflag, TAG_DONE);
  2250.                     break;
  2251.                     case ESCAPE:
  2252.                         if (verify())
  2253.                             cleanexit(EXIT_SUCCESS);
  2254.                         break;
  2255.                     case HELP:
  2256.                         helpabout();
  2257.                         break;
  2258.                     case INTERNATIONALONE:
  2259.                         if (lisa == 2)
  2260.                         {    say(FIRSTLISA, RED);
  2261.                             anykey(FALSE);
  2262.                             lisa = 0;
  2263.                         } else
  2264.                             lisa = 1;
  2265.                         break;
  2266.                     case INTERNATIONALTWO:
  2267.                         if (lisa == 1)
  2268.                         {    say(SECONDLISA, BLUE);
  2269.                             anykey(FALSE);
  2270.                             lisa = 0;
  2271.                         } else
  2272.                             lisa = 2;
  2273.                         break;
  2274.                     default:
  2275.                         break;
  2276.                     }
  2277.                 break;
  2278.             case IDCMP_MENUPICK:
  2279.                 while (code != MENUNULL)
  2280.                 {    ItemPtr = ItemAddress(MenuPtr, code);
  2281.                     switch (MENUNUM(code))
  2282.                     {
  2283.                     case MN_PROJECT:
  2284.                         switch (ITEMNUM(code))
  2285.                         {
  2286.                         case IN_NEW:
  2287.                             effect(FXFILENEW);
  2288.                             newfields();
  2289.                             say("New done.", WHITE);
  2290.                             anykey(TRUE);
  2291.                             say(objectdesc[object], WHITE);
  2292.                         break;
  2293.                             case IN_OPEN:
  2294.                                 effect(FXFILEOPEN);
  2295.                                 fileopen(FALSE);
  2296.                                 say(objectdesc[object], WHITE);
  2297.                             break;
  2298.                             case IN_REVERT:
  2299.                                 fileopen(TRUE);
  2300.                                 say(objectdesc[object], WHITE);
  2301.                             break;
  2302.                             case IN_SAVE:
  2303.                                 effect(FXFILESAVE);
  2304.                                 filesaveas(FALSE);
  2305.                                 say(objectdesc[object], WHITE);
  2306.                             break;
  2307.                             case IN_SAVEAS:
  2308.                                 effect(FXFILESAVEAS);
  2309.                                 filesaveas(TRUE);
  2310.                                 say(objectdesc[object], WHITE);
  2311.                             break;
  2312.                             case IN_PROJECTDELETE:
  2313.                                 effect(FXEDITDELETE);
  2314.                                 filedelete();
  2315.                             break;
  2316.                             case IN_QUIT:
  2317.                                 if (verify())
  2318.                                     cleanexit(EXIT_SUCCESS);
  2319.                             break;
  2320.                             default:
  2321.                             break;
  2322.                             }
  2323.                         break;
  2324.                         case MN_SETTINGS:
  2325.                         switch(ITEMNUM(code))
  2326.                         {
  2327.                         case IN_CREATEICONS:
  2328.                             if (ItemPtr->Flags & CHECKED)
  2329.                                 icons = TRUE;
  2330.                             else icons = FALSE;
  2331.                         break;
  2332.                         default:
  2333.                         break;
  2334.                         }
  2335.                     break;
  2336.                     case MN_HELP:
  2337.                         switch(ITEMNUM(code))
  2338.                         {
  2339.                         case IN_MANUAL:
  2340.                             helpmanual();
  2341.                         break;
  2342.                         case IN_ABOUT:
  2343.                 helpabout();
  2344.             break;
  2345.                         case IN_CREATURES:
  2346.                             help(ORB);
  2347.                         break;
  2348.                         case IN_OBJECTS:
  2349.                             help(AFFIXER);
  2350.                         break;
  2351.                         }
  2352.             break;
  2353.                     default:
  2354.             break;
  2355.             }
  2356.             code = ItemPtr->NextSelect;
  2357.         }
  2358.         break;
  2359.             case IDCMP_MOUSEBUTTONS:
  2360.                 if (code == SELECTDOWN)
  2361.                     if (ignore)
  2362.                         ignore = FALSE;
  2363.                     else a = PLAYGAME;
  2364.                 break;
  2365.             case IDCMP_REFRESHWINDOW:
  2366.                 GT_BeginRefresh(MainWindowPtr);
  2367.                 GT_EndRefresh(MainWindowPtr, TRUE);
  2368.                 break;
  2369.             case IDCMP_GADGETUP:
  2370.                 if (WhichGadgetPtr == CheckboxGadgetPtr)
  2371.                     iso = !iso;
  2372.                                 elif (WhichGadgetPtr == RandomGadgetPtr)
  2373.                                     randomflag = !randomflag;
  2374.                 else
  2375.                 {    for (player = 0; player <= 3; player++)
  2376.                         if (WhichGadgetPtr == CycleGadgetPtr[player])
  2377.                         {    if (code == JOYSTICK && player == 2 && (!joy))
  2378.                             {    if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
  2379.                                     worm[2].control = NONE;
  2380.                                 else
  2381.                                     worm[2].control = AMIGA;
  2382.                                 GT_SetGadgetAttrs(CycleGadgetPtr[2], MainWindowPtr, NULL, GTCY_Active, worm[2].control, TAG_DONE);
  2383.                             } else
  2384.                                 worm[player].control = code;
  2385.                             break;
  2386.                         }
  2387.                 }
  2388.                 break;
  2389.             case IDCMP_ACTIVEWINDOW:
  2390.                 ignore = TRUE;
  2391.                 break;
  2392.             case IDCMP_CLOSEWINDOW:
  2393.                 cleanexit(EXIT_SUCCESS);
  2394.                 break;
  2395.             default:
  2396.                 /* IDCMP_MENUVERIFY, IDCMP_INTUITICKS */
  2397.                 break;
  2398.         }    }
  2399.         if (joy && GetMsg(JoyPortPtr))
  2400.         {    if (GameEvent.ie_Code == IECODE_LBUTTON)
  2401.                 a = PLAYGAME;
  2402.             sendreadrequest();
  2403.         }
  2404.         if (a == PLAYGAME)
  2405.         {    if (worm[0].control == NONE && worm[1].control == NONE && worm[2].control == NONE && worm[3].control == NONE)
  2406.             {    say("No worms active!", WHITE);
  2407.                 anykey(TRUE);
  2408.                 a = GAMEOVER;
  2409.         }    }
  2410.                 // CheckIO() returns NULL if complete, pointer if incomplete.
  2411.                 if (CheckIO((struct IORequest *) TimerRqPtr))
  2412.             draw(CLOCKICON, ICONY, CLOCK);
  2413.         else draw(CLOCKICON, ICONY, BLACKENED);
  2414.         WaitIO((struct IORequest *) TimerRqPtr);
  2415.     } while (a == GAMEOVER);
  2416.  
  2417.     for (player = 0; player <= 3; player++)
  2418.         GT_SetGadgetAttrs(CycleGadgetPtr[player], MainWindowPtr, NULL, GA_Disabled, TRUE, TAG_DONE);
  2419.     GT_SetGadgetAttrs(CheckboxGadgetPtr, MainWindowPtr, NULL, GA_Disabled, TRUE, TAG_DONE);
  2420.     GT_SetGadgetAttrs(RandomGadgetPtr, MainWindowPtr, NULL, GA_Disabled, TRUE, TAG_DONE);
  2421.     Image.ImageData = ImageData[BLACKENED];
  2422.     DrawImage(MainWindowPtr->RPort, &Image, FIRSTDESCX, DESCY);
  2423.     DrawImage(MainWindowPtr->RPort, &Image, SECONDDESCX, DESCY);
  2424.  
  2425.     if (a == FIELDEDIT)
  2426.         fieldedit();
  2427.     else
  2428.     {   newgame();
  2429.         clearkybd();
  2430.         Forbid();
  2431.         MainWindowPtr->Flags |= WFLG_RMBTRAP;
  2432.         Permit();
  2433. }   }
  2434.  
  2435. AGLOBAL void toggle(SBYTE key)
  2436. {   PERSIST ABOOL songstarted = FALSE;
  2437.  
  2438.     switch(key)
  2439.     {
  2440.     case F:
  2441.         if (mode == FX) /* F in FX mode: no sound */
  2442.         {   freefx();
  2443.             mode = FALSE;
  2444.             draw(MUSICICON, ICONY, BLACKENED);
  2445.         } else if (fxable != FAILED) /* F otherwise: change to FX mode */
  2446.         {   if (mode == MUSIC) /* stop any music that is playing */
  2447.             {   StopPlayer();
  2448.                 FreePlayer();
  2449.             }
  2450.             if (fxable == DEFER) /* load samples if needed */
  2451.                 loadthefx();
  2452.             if (fxable == SUCCEEDED) /* if we have samples in memory */
  2453.             {   if (beginfx())
  2454.                 {   mode = FX;
  2455.                     effect(FXLAUNCH);
  2456.                     draw(MUSICICON, ICONY, FX);
  2457.         }   }   }
  2458.     break;
  2459.     case M:
  2460.         if (mode == MUSIC) /* M in MUSIC mode: no sound */
  2461.         {   StopPlayer();
  2462.             FreePlayer();
  2463.             mode = FALSE;
  2464.             draw(MUSICICON, ICONY, BLACKENED);
  2465.         } else if (musicable != FAILED) /* M otherwise: change to music mode */
  2466.         {   if (mode == FX) /* stop any samples that are playing */
  2467.                 freefx();
  2468.             /* Of course, these statements are ordered in this
  2469.             way for a reason, so don't change it. :-) */
  2470.             if (musicable == DEFER)
  2471.                 loadthemusic();
  2472.             if (musicable == SUCCEEDED)
  2473.             {   if (GetPlayer(0))
  2474.                 {   say("No channels for music!", RED);
  2475.                     anykey(TRUE);
  2476.                     mode = FALSE;
  2477.                     draw(MUSICICON, ICONY, BLACKENED);
  2478.                 } else
  2479.                 {   if (songstarted)
  2480.                         ContModule(SongPtr);
  2481.                     else
  2482.                     {   PlayModule(SongPtr);
  2483.                         songstarted = TRUE;
  2484.                     }
  2485.                     mode = MUSIC;
  2486.                     draw(MUSICICON, ICONY, MUSIC);
  2487.         }   }   }
  2488.     break;
  2489.     default:
  2490.         flash(2);
  2491.         // assert(0);
  2492.     break;
  2493. }   }
  2494.  
  2495. ABOOL verify(void)
  2496. {    pausetimer();
  2497.     if (modified && (EasyRequest(MainWindowPtr, &EasyStruct, NULL) == 0))
  2498.         return FALSE;
  2499.     else return TRUE;
  2500.     unpausetimer();
  2501. }
  2502.  
  2503. void waitasec(void)
  2504. {   Delay(50);
  2505. }
  2506.  
  2507. void systemsetup(void)
  2508. {    worm[0].control    = NONE;
  2509.     worm[1].control    = KEYBOARD;
  2510.     worm[2].control    = NONE;
  2511.     worm[3].control = AMIGA;
  2512. }
  2513.  
  2514. ABOOL ZOpen(STRPTR fieldname, ABOOL write)
  2515. {    if (!write)
  2516.         if (FilePtr = Open(fieldname, MODE_OLDFILE))
  2517.             return TRUE;
  2518.         else return FALSE;
  2519.     else
  2520.         if (FilePtr = Open(fieldname, MODE_NEWFILE))
  2521.             return TRUE;
  2522.         else return FALSE;
  2523. }
  2524. ABOOL ZRead(STRPTR IOBuffer, ULONG length)
  2525. {    if (Read(FilePtr, IOBuffer, length) == length)
  2526.         return TRUE;
  2527.     else return FALSE;
  2528. }
  2529. ABOOL ZWrite(STRPTR IOBuffer, ULONG length)
  2530. {    if (Write(FilePtr, IOBuffer, length) == length)
  2531.         return TRUE;
  2532.     else return FALSE;
  2533. }
  2534. ABOOL ZClose(void)
  2535. {    if (Close(FilePtr))
  2536.     {    FilePtr = NULL;
  2537.         return TRUE;
  2538.     } else
  2539.     {    /* "If Close() returns DOSFALSE, the user has already cancelled an
  2540.         error requester and the function has already freed the FileHandle
  2541.         (and even marked it so any attempt to close it again will bring up
  2542.         the "Software Failure" requester). Therefore FilePtr should be set
  2543.         to zero in any case." - Jilles Tjoelker. */
  2544.  
  2545.         FilePtr = NULL;
  2546.         return FALSE;
  2547. }    }
  2548.  
  2549. void timing(void)
  2550. {    ;
  2551. }
  2552.  
  2553. MODULE void loadthefx(void)
  2554. {   UBYTE*        p8data;
  2555.     TEXT          saystring[SAYLIMIT + 1];
  2556.     SBYTE         code = 0, i,
  2557.                   iobuffer[8]; /* buffer for 8SVX.VHDR  */
  2558.     SBYTE*        psample[2];  /* sample pointers */
  2559.     struct Chunk* p8Chunk;     /* pointers for 8SVX parsing */
  2560.     Voice8Header* pVoice8Header = NULL; // only set to NULL to avoid spurious warnings
  2561.     ULONG         rd8count;
  2562.  
  2563.     say("Loading sound effects...", WHITE);
  2564.     fxable = SUCCEEDED;
  2565.  
  2566.     for (i = 0; i <= SAMPLES; i++)
  2567.         samp[i].base = NULL;
  2568.  
  2569.     for (i = 0; i <= SAMPLES; i++)
  2570.     {   if (!(FilePtr = Open(samp[i].filename, MODE_OLDFILE)))
  2571.             code = 1;                               /* can't open file */
  2572.         else
  2573.         {   rd8count = Read(FilePtr, iobuffer, 8L);
  2574.             if (rd8count == -1)
  2575.                 code = 2;                           /* can't read file */
  2576.             elif (rd8count < 8)
  2577.                 code = 3;                           /* not an IFF 8SVX; too short */
  2578.             else
  2579.             {   p8Chunk = (struct Chunk *) iobuffer;
  2580.                 if (p8Chunk->ckID != ID_FORM)
  2581.                     code = 4;                       /* not an IFF FORM */
  2582.                 elif (!(fbase = (UBYTE *) AllocMem(fsize = p8Chunk->ckSize, MEMF_PUBLIC | MEMF_CLEAR)))
  2583.                     code = 5;                       /* no memory for read */
  2584.                 else
  2585.                 {   p8data = fbase;
  2586.                     rd8count = Read(FilePtr, p8data, p8Chunk->ckSize);
  2587.                     if (rd8count == -1)
  2588.                         code = 6;                   /* read error */
  2589.                     elif (rd8count < p8Chunk->ckSize)
  2590.                         code = 7;                   /* malformed IFF; too short */
  2591.                     elif (MAKE_ID(*p8data, *(p8data + 1), *(p8data + 2), *(p8data + 3)) != ID_8SVX)
  2592.                         code = 8;                   /* not an IFF 8SVX */
  2593.                     else
  2594.                     {   p8data = p8data + 4;
  2595.                         while (p8data < fbase + fsize)
  2596.                         {   p8Chunk = (struct Chunk *) p8data;
  2597.                             switch(p8Chunk->ckID)
  2598.                             {
  2599.                             case ID_VHDR:
  2600.                                 pVoice8Header     = (Voice8Header *) (p8data + 8L);
  2601.                             break;
  2602.                             case ID_BODY:
  2603.                                 psample[0]        = (SBYTE *) (p8data + 8L);
  2604.                                 psample[1]        = psample[0] + pVoice8Header->oneShotHiSamples;
  2605.                                 samp[i].length[0] = (ULONG) pVoice8Header->oneShotHiSamples;
  2606.                                 samp[i].length[1] = (ULONG) pVoice8Header->repeatHiSamples;
  2607.  
  2608.                                 /* To grab the volume level from the IFF
  2609.                                 8SVX file itself, add this line here:
  2610.  
  2611.                                 samp[i].volume    = (SBYTE) (pVoice8Header->volume / 156); */
  2612.                             break;
  2613.                             default:
  2614.                             break;
  2615.                             }
  2616.                             p8data += 8L + p8Chunk->ckSize;
  2617.                             if (p8Chunk->ckSize & 1L == 1)
  2618.                                 p8data++;
  2619.                         }
  2620.                         if (samp[i].length[0] == 0)
  2621.                             samp[i].bank = 1;
  2622.                         else samp[i].bank = 0;
  2623.                         if (samp[i].length[samp[i].bank] <= 102400)
  2624.                             samp[i].size = samp[i].length[samp[i].bank];
  2625.                         else samp[i].size = 102400;
  2626.                         samp[i].base = (UBYTE *) AllocMem(samp[i].size, MEMF_CHIP | MEMF_CLEAR);
  2627.                         if (!samp[i].base)
  2628.                             code = 9; /* no chip memory */
  2629.                         else
  2630.                         {   CopyMem(psample[samp[i].bank], samp[i].base, samp[i].size);
  2631.                             psample[samp[i].bank] += samp[i].size;
  2632.                             samp[i].speed = PALCLOCK / pVoice8Header->samplesPerSec;
  2633.                             /* perhaps we should have a different
  2634.                             value for DblPAL screens? */
  2635.                             if (fbase)
  2636.                             {   FreeMem(fbase, fsize);
  2637.                                 fbase = NULL;
  2638.                             }
  2639.                             if (FilePtr)
  2640.                             {   Close(FilePtr);
  2641.                                 FilePtr = NULL;
  2642.         }   }   }   }   }   }
  2643.         if (code)
  2644.         {   freefx();
  2645.             fxable = FAILED;
  2646.             strcpy(saystring, samp[i].filename);
  2647.             strcat(saystring, ": ");
  2648.             strcat(saystring, sfxerror[code]);
  2649.             say(saystring, RED);
  2650.             anykey(TRUE);
  2651.             break;
  2652. }   }   }
  2653.  
  2654. MODULE void loadthemusic(void)
  2655. {    if (!(MEDPlayerBase = (struct MEDPlayerBase *) OpenLibrary("medplayer.library", 0L)))
  2656.     {    say("Can't open MEDPlayer.library!", RED);
  2657.         anykey(TRUE);
  2658.     } else
  2659.     {    say("Loading music...", WHITE);
  2660.         if (SongPtr = (struct MMD0 *) LoadModule("PROGDIR:WormWars.MED"))
  2661.                         musicable = SUCCEEDED;
  2662.         else
  2663.         {    say("Can't load music!", RED);
  2664.             anykey(TRUE);
  2665. }    }    }
  2666.  
  2667. void clearscreen(void)
  2668. {   SBYTE i;
  2669.  
  2670.     Background.PlaneOnOff = 8 + ((rand() % 4) * 2);
  2671.     // random background colour
  2672.     DrawImage
  2673.     (   MainWindowPtr->RPort,
  2674.         &Background,
  2675.         0,
  2676.         0
  2677.     );
  2678.     if (mode == MUSIC)
  2679.         draw(MUSICICON, ICONY, MUSIC);
  2680.     else if (mode == FX)
  2681.         draw(MUSICICON, ICONY, FX);
  2682.     else draw(MUSICICON, ICONY, BLACKENED);
  2683.     draw(CLOCKICON, ICONY, CLOCK);
  2684.  
  2685.     if (a != FIELDEDIT)
  2686.     {   if (!iso)
  2687.     {   for (i = 0; i <= 3; i++)
  2688.         {   if (worm[i].control != NONE)
  2689.                 {   Image.ImageData = ImageData[BONUS];
  2690.                     DrawImage
  2691.                     (   MainWindowPtr->RPort,
  2692.                         &Image,
  2693.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2694.             STARTYPIXEL + 1               + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2695.                     );
  2696.                     Image.ImageData = ImageData[LIFE];
  2697.                     DrawImage
  2698.                     (   MainWindowPtr->RPort,
  2699.                         &Image,
  2700.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2701.                         STARTYPIXEL + 1 + (1 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2702.                     );
  2703.                     Image.ImageData = ImageData[BIAS];
  2704.                     DrawImage
  2705.                     (   MainWindowPtr->RPort,
  2706.                         &Image,
  2707.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2708.                         STARTYPIXEL + 1 + (2 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2709.                     );
  2710.                     Image.ImageData = ImageData[NITRO];
  2711.                     DrawImage
  2712.                     (   MainWindowPtr->RPort,
  2713.                         &Image,
  2714.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2715.                         STARTYPIXEL + 1 + (3 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2716.                     );
  2717.                     Image.ImageData = ImageData[AMMO];
  2718.                     DrawImage
  2719.                     (   MainWindowPtr->RPort,
  2720.                         &Image,
  2721.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2722.                         STARTYPIXEL + 1 + (5 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2723.                     );
  2724.                     Image.ImageData = ImageData[POWER];
  2725.                     DrawImage
  2726.                     (   MainWindowPtr->RPort,
  2727.                         &Image,
  2728.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2729.                         STARTYPIXEL + 1 + (6 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2730.                     );
  2731.                     Image.ImageData = ImageData[ARMOUR];
  2732.                     DrawImage
  2733.                     (   MainWindowPtr->RPort,
  2734.                         &Image,
  2735.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2736.                         STARTYPIXEL + 1 + (8 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2737.                     );
  2738.                     Image.ImageData = ImageData[TONGUE];
  2739.                     DrawImage
  2740.                     (   MainWindowPtr->RPort,
  2741.                         &Image,
  2742.                         (worm[i].statx * ENDXPIXEL) + FONTX + 4,
  2743.                         STARTYPIXEL + 1 + (9 * FONTY) + (worm[i].staty * (ENDYPIXEL - 1 - (11 * FONTY)))
  2744.                     );
  2745.         }   }   }
  2746.         else
  2747.         {   // assert(iso);
  2748.             if (worm[0].control != NONE || worm[2].control != NONE)
  2749.             {   Image.ImageData = ImageData[BONUS];
  2750.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 4);
  2751.                 Image.ImageData = ImageData[LIFE];
  2752.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 12);
  2753.                 Image.ImageData = ImageData[BIAS];
  2754.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 20);
  2755.                 Image.ImageData = ImageData[NITRO];
  2756.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 28);
  2757.                 Image.ImageData = ImageData[AMMO];
  2758.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 36);
  2759.                 Image.ImageData = ImageData[POWER];
  2760.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 44);
  2761.                 Image.ImageData = ImageData[ARMOUR];
  2762.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 52);
  2763.                 Image.ImageData = ImageData[TONGUE];
  2764.                 DrawImage(MainWindowPtr->RPort, &Image, 54, 60);
  2765.             }
  2766.             if (worm[1].control != NONE || worm[3].control != NONE)
  2767.             {   Image.ImageData = ImageData[BONUS];
  2768.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 183);
  2769.                 Image.ImageData = ImageData[LIFE];
  2770.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 191);
  2771.                 Image.ImageData = ImageData[BIAS];
  2772.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 199);
  2773.                 Image.ImageData = ImageData[NITRO];
  2774.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 207);
  2775.                 Image.ImageData = ImageData[AMMO];
  2776.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 215);
  2777.                 Image.ImageData = ImageData[POWER];
  2778.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 223);
  2779.                 Image.ImageData = ImageData[ARMOUR];
  2780.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 231);
  2781.                 Image.ImageData = ImageData[TONGUE];
  2782.                 DrawImage(MainWindowPtr->RPort, &Image, 579, 239);
  2783.         }   }
  2784.         for (i = 0; i <= 3; i++)
  2785.         {   icon(i, ICE);
  2786.             icon(i, REMNANTS);
  2787.             icon(i, AFFIXER);
  2788.             icon(i, SIDESHOT);
  2789.             icon(i, PUSHER);
  2790.             icon(i, FREEDOM);
  2791.             icon(i, CUTTER);
  2792. }   }   }
  2793.  
  2794. void datestamp(void)
  2795. {   ULONG            seconds, micros;
  2796.     struct ClockData Date;
  2797.     TEXT             temp[5];
  2798.  
  2799.     CurrentTime(&seconds, µs);
  2800.     Amiga2Date(seconds, &Date);
  2801.     stci_d(times, Date.hour);            /* hh */
  2802.     align(times, 2, ' ');
  2803.     times[2] = ':';                     /* hh: */
  2804.     times[3] = 0;
  2805.     stci_d(temp, Date.min);
  2806.     align(temp, 2, '0');
  2807.     temp[2] = 0;
  2808.     strcat(times, temp);                /* hh:mm */
  2809.  
  2810.     stci_d(date, Date.mday);            /* dd */
  2811.     align(date, 2, ' ');
  2812.     date[2] = '/';
  2813.     date[3] = 0;                        /* dd/ */
  2814.     stci_d(temp, Date.month);
  2815.     align(temp, 2, ' ');
  2816.     temp[2] = 0;
  2817.     strcat(date, temp);                    /* dd/mm */
  2818.     strcat(date, "/");                    /* dd/mm/ */
  2819.     stci_d(temp, Date.year);
  2820.     temp[0] = temp[2];
  2821.     temp[1] = temp[3];
  2822.     temp[2] = 0;
  2823.     strcat(date, temp);                    /* dd/mm/yy */
  2824. }
  2825.  
  2826. void turborender(void)
  2827. {   UBYTE random = rand() % 3;
  2828.     SBYTE x, y;
  2829.  
  2830.     if (a != PLAYGAME || (!level) || !(randomflag))
  2831.         sourcelevel = level;
  2832.  
  2833.     switch(random)
  2834.     {
  2835.     case 0:
  2836.         for (x = 0; x <= (FIELDX / 2) + 1; x++)
  2837.             for (y = 0; y <= FIELDY / 2; y++)
  2838.             {   draw(x, y, board[sourcelevel][x][y]);
  2839.                 draw(FIELDX - x, y, board[sourcelevel][FIELDX - x][y]);
  2840.                 draw(x, FIELDY - y, board[sourcelevel][x][FIELDY - y]);
  2841.                 draw(FIELDX - x, FIELDY - y, board[sourcelevel][FIELDX - x][FIELDY - y]);
  2842.             }
  2843.     break;
  2844.     case 1:
  2845.         for (y = 0; y <= FIELDY / 2; y++)
  2846.             for (x = 0; x <= (FIELDX / 2) + 1; x++)
  2847.             {   draw(x, y, board[sourcelevel][x][y]);
  2848.                 draw(FIELDX - x, y, board[sourcelevel][FIELDX - x][y]);
  2849.                 draw(x, FIELDY - y, board[sourcelevel][x][FIELDY - y]);
  2850.                 draw(FIELDX - x, FIELDY - y, board[sourcelevel][FIELDX - x][FIELDY - y]);
  2851.             }
  2852.     break;
  2853.     case 2:
  2854.         x = y = 0;
  2855.         do
  2856.         {   draw(x, y, board[sourcelevel][x][y]);
  2857.             x += APPEAR_CONSTANT;
  2858.             if (x >= FWIDTH)
  2859.             {   x -= FWIDTH;
  2860.                 y++;
  2861.                 if (y >= FHEIGHT)
  2862.                     y = 0;
  2863.         }   }
  2864.         while (x || y);
  2865.     break;
  2866.     default:
  2867.         assert(0);
  2868.     break;
  2869.     }
  2870.  
  2871.     if (a == FIELDEDIT)
  2872.     {   draw(startx[sourcelevel], starty[sourcelevel], START);
  2873.     } else
  2874.     {   for (y = 0; y <= FIELDY; y++)
  2875.             draw(ARROWX, y, BLACKENED);
  2876. }   }
  2877.  
  2878. MODULE void parsewb(void)
  2879. {   struct DiskObject* DiskObject;
  2880.     char**             ToolArray;
  2881.     char*              s;
  2882.  
  2883.     if ((*WBArg->wa_Name) && (DiskObject = GetDiskObject(WBArg->wa_Name)))
  2884.     {   ToolArray = (char **) DiskObject->do_ToolTypes;
  2885.  
  2886.         if (s = (char *) FindToolType(ToolArray, "NOFX"))
  2887.             fxable = DEFER;
  2888.         if (s = (char *) FindToolType(ToolArray, "NOMUSIC"))
  2889.             musicable = DEFER;
  2890.         if (s = (char *) FindToolType(ToolArray, "NOICONS"))
  2891.             icons = FALSE;
  2892.         if (s = (char *) FindToolType(ToolArray, "OVERHEAD"))
  2893.             iso = FALSE;
  2894.         if (s = (char *) FindToolType(ToolArray, "SHUFFLE"))
  2895.             randomflag = TRUE;
  2896.         if (s = (STRPTR) FindToolType(ToolArray, "QUIET"))
  2897.         {   quiet = TRUE;
  2898.         }
  2899.         if (s = (char *) FindToolType(ToolArray, "FILE"))
  2900.             strcpy(pathname, WBArg->wa_Name);
  2901.         if (s = (char *) FindToolType(ToolArray, "GREEN"))
  2902.         {   if (MatchToolValue(s, "KYBD"))
  2903.                 worm[0].control = KEYBOARD;
  2904.             elif (MatchToolValue(s, "JOY"))
  2905.                 worm[0].control = JOYSTICK;
  2906.             elif (MatchToolValue(s, "AMIGA"))
  2907.             {   worm[0].control = AMIGA;
  2908.             } elif (MatchToolValue(s, "NONE"))
  2909.             {   worm[0].control = NONE;
  2910.         }   }
  2911.         if (s = (char *) FindToolType(ToolArray, "RED"))
  2912.         {   if (MatchToolValue(s, "KYBD"))
  2913.             {   worm[1].control = KEYBOARD;
  2914.             } elif (MatchToolValue(s, "JOY"))
  2915.             {   worm[1].control = JOYSTICK;
  2916.             } elif (MatchToolValue(s, "AMIGA"))
  2917.             {   worm[1].control = AMIGA;
  2918.             } elif (MatchToolValue(s, "NONE"))
  2919.             {   worm[1].control = NONE;
  2920.         }   }
  2921.         if (s = (char *) FindToolType(ToolArray, "BLUE"))
  2922.         {   if (MatchToolValue(s, "JOY"))
  2923.                 worm[2].control = JOYSTICK;
  2924.             elif (MatchToolValue(s, "AMIGA"))
  2925.                 worm[2].control = AMIGA;
  2926.             elif (MatchToolValue(s, "NONE"))
  2927.                 worm[2].control = NONE;
  2928.         }
  2929.         if (s = (char *) FindToolType(ToolArray, "YELLOW"))
  2930.         {   if (MatchToolValue(s, "JOY"))
  2931.                 worm[2].control = JOYSTICK;
  2932.             elif (MatchToolValue(s, "AMIGA"))
  2933.                 worm[3].control = AMIGA;
  2934.             elif (MatchToolValue(s, "NONE"))
  2935.                 worm[3].control = NONE;
  2936.         }
  2937.         FreeDiskObject(DiskObject);
  2938. }   }
  2939.  
  2940. MODULE void pausetimer(void)
  2941. {    GetSysTime(CurrentValPtr);
  2942. }
  2943. MODULE void unpausetimer(void)
  2944. {    GetSysTime(PausedValPtr);
  2945.     SubTime(PausedValPtr, CurrentValPtr);
  2946.     AddTime(StartValPtr, PausedValPtr);
  2947. }
  2948.  
  2949. MODULE UBYTE ReadJoystick(UWORD joynum)
  2950. {   extern struct Custom far custom;
  2951.     UBYTE ret = 0;
  2952.     UWORD joy;
  2953.  
  2954.     if (joynum == 0)
  2955.         joy = custom.joy0dat;
  2956.     else joy = custom.joy1dat;
  2957.  
  2958.     ret += (joy >> 1 ^ joy) & 0x0100 ? JOYUP : 0;  
  2959.     ret += (joy >> 1 ^ joy) & 0x0001 ? JOYDOWN : 0;
  2960.     ret += joy & 0x0200 ? JOYLEFT : 0;
  2961.     ret += joy & 0x0002 ? JOYRIGHT : 0;
  2962.  
  2963.     if (joynum == 0)
  2964.     {   ret += !(CIAPtr->ciapra & 0x0040) ? JOYFIRE1 : 0; /* read firebuttons */
  2965.         ret += !(POTGOR & 0x0400) ? JOYFIRE2 : 0;         /* on joyport 0 */
  2966.     } else
  2967.     {   ret += !(CIAPtr->ciapra & 0x0080) ? JOYFIRE1 : 0; /* read firebuttons */
  2968.         ret += !(POTGOR & 0x4000) ? JOYFIRE2 : 0;         /* on joyport 1 */
  2969.     }
  2970.  
  2971.     return(ret);
  2972. }
  2973.  
  2974. AGLOBAL void ReadAdapterJoystick(UBYTE unit)
  2975. {   AUTO    UBYTE firebit, addroffset, snapshot;
  2976.     AUTO    SBYTE xx = 0, yy = 0;
  2977.     AUTO    ABOOL fire;
  2978.     PERSIST UBYTE oldsnapshot[2] = {0, 0};
  2979.     PERSIST ABOOL oldfire[2]     = {0, 0};
  2980.     PERSIST UBYTE *fireaddrPtr,
  2981.                   *moveaddrPtr;
  2982.  
  2983.     fireaddrPtr = 0xBFD000;
  2984.     moveaddrPtr = 0xBFE101;
  2985.  
  2986. /*  // Those pointers could easily be auto or constant or whatever; they
  2987.     // never change.
  2988.  
  2989.     bit 7 of $bfe101 is right of joystick '4'
  2990.     bit 6 of $bfe101 is left  of joystick '4'
  2991.     bit 5 of $bfe101 is down  of joystick '4'
  2992.     bit 4 of $bfe101 is up    of joystick '4'
  2993.     bit 3 of $bfe101 is right of joystick '3'
  2994.     bit 2 of $bfe101 is left  of joystick '3'
  2995.     bit 1 of $bfe101 is down  of joystick '3'
  2996.     bit 0 of $bfe101 is up    of joystick '3'
  2997.  
  2998.     bit 2 of $bfd000 is fire  of joystick '3'
  2999.     bit 0 of $bfd000 is fire  of joystick '4'
  3000.  
  3001.     unit: number of the unit (2 = '3', 3 = '4') */
  3002.  
  3003.     if (unit == 2)
  3004.     {   firebit = 2;
  3005.         addroffset = 0;
  3006.     } else
  3007.     {   // assert(unit == 3);
  3008.         firebit = 0;
  3009.         addroffset = 4;
  3010.     }
  3011.  
  3012.     if (worm[unit].control == JOYSTICK && worm[unit].lives)
  3013.     {   if ((*fireaddrPtr) & (1 << firebit))
  3014.             fire = TRUE;
  3015.  
  3016.         snapshot = *moveaddrPtr; // we cache this address in the snapshot
  3017.                                  // variable, so that joystick reading is an
  3018.                                  // atomic operation
  3019.         if (snapshot & (1 << (addroffset + 3)))
  3020.             xx = 1;
  3021.         elif (snapshot & (1 << (addroffset + 2)))
  3022.             xx = -1;
  3023.         if (snapshot & (1 << (addroffset + 1)))
  3024.             yy = 1;
  3025.         elif (snapshot & (1 <<  addroffset     ))
  3026.             yy = -1;
  3027.  
  3028.         if (oldsnapshot[unit - 2] != snapshot || oldfire[unit - 2] != fire)
  3029.         {   if (fire)
  3030.                 wormqueue(unit, 0, 0);
  3031.             else
  3032.                 wormqueue(unit, xx, yy);
  3033.         }
  3034.         oldsnapshot[unit - 2] = snapshot;
  3035.         oldfire[unit - 2]     = fire;
  3036. }   }
  3037.  
  3038. void joy0(void)
  3039. {   AUTO    UBYTE joyval;
  3040.     AUTO    ABOOL fire = FALSE;
  3041.     AUTO    SBYTE xx = 0, yy = 0;
  3042.     PERSIST ABOOL oldjoyval;
  3043.  
  3044.     if (worm[3].control == JOYSTICK && worm[3].lives)
  3045.     {   joyval = ReadJoystick(0);
  3046.         if (joyval != oldjoyval)
  3047.         {   if (joyval & JOYUP)
  3048.                 yy = -1;
  3049.             elif (joyval & JOYDOWN)
  3050.                 yy = 1;
  3051.             if (joyval & JOYLEFT)
  3052.                 xx = -1;
  3053.             elif (joyval & JOYRIGHT)
  3054.                 xx = 1;
  3055.             if ((joyval & JOYFIRE1) || (joyval & JOYFIRE2))
  3056.                 fire = TRUE;
  3057.             if (joyval != 0)              // if joystick is off-centre or firebutton is down
  3058.             {   if (fire)                 // if firebutton is down
  3059.                     wormqueue(3, 0, 0);   // then shoot/jump
  3060.                 else                      // if firebutton is up
  3061.                     wormqueue(3, xx, yy); // then move
  3062.         }   }
  3063.         oldjoyval = joyval;
  3064. }   }
  3065.  
  3066. void flash(ULONG where)
  3067. {   TEXT saystring[SAYLIMIT + 1];
  3068.  
  3069.     stci_d(saystring, where);
  3070.     say(saystring, PURPLE);
  3071.     while(1);
  3072. }
  3073.  
  3074.